r563: Ogg theora+vorbis export added
[cinelerra_cv/mob.git] / quicktime / mpeg4.c
blobeedefd0c3e3def7fc7019b3dac03e603d9695430
1 /* General codec for all MPEG-4 derived encoding. */
2 /* Uses ffmpeg and encore50. */
3 /* Encore50 still seemed to provide better results than ffmpeg for encoding */
4 /* so it does all the generic MPEG-4 encoding. */
10 #include <ffmpeg/avcodec.h>
11 #include "colormodels.h"
12 #include "funcprotos.h"
13 #include "quicktime.h"
14 #include "workarounds.h"
15 #include ENCORE_INCLUDE
16 //#include DECORE_INCLUDE
20 #include <pthread.h>
21 #include <stdint.h>
22 #include <stdlib.h>
24 #define FRAME_RATE_BASE 10000
27 typedef struct
29 #define FIELDS 2
30 int decode_initialized[FIELDS];
33 // FFMpeg internals
34 AVCodec *decoder[FIELDS];
35 AVCodecContext *decoder_context[FIELDS];
36 AVFrame picture[FIELDS];
39 // Decore internals
40 // DEC_PARAM dec_param[FIELDS];
41 // int decode_handle[FIELDS];
43 // Last frame decoded
44 long last_frame[FIELDS];
46 int got_key[FIELDS];
57 int encode_initialized[FIELDS];
58 // Information for picking the right library routines.
59 // ID out of avcodec.h for the codec used.
60 // Invalid if encore50 is used.
61 int ffmpeg_id;
62 // True if encore50 is being used.
63 int use_encore;
65 // FFMpeg internals
66 AVCodec *encoder[FIELDS];
67 AVCodecContext *encoder_context[FIELDS];
70 // Encore internals
71 int encode_handle[FIELDS];
72 ENC_PARAM enc_param[FIELDS];
73 // Must count pframes in VBR
74 int p_count[FIELDS];
77 // Encoding parameters
78 int bitrate;
79 // For heroine 60 encoding, we want different streams for each field.
80 int total_fields;
81 long rc_period; // the intended rate control averaging period
82 long rc_reaction_period; // the reation period for rate control
83 long rc_reaction_ratio; // the ratio for down/up rate control
84 long max_key_interval; // the maximum interval between key frames
85 int bitrate_tolerance;
86 int interlaced;
87 int gop_size;
88 int max_quantizer; // the upper limit of the quantizer
89 int min_quantizer; // the lower limit of the quantizer
90 int quantizer; // For vbr
91 int quality; // the forward search range for motion estimation
92 int fix_bitrate;
93 int use_deblocking;
96 // Temporary storage for color conversions
97 char *temp_frame;
98 // Storage of compressed data
99 unsigned char *work_buffer;
100 // Allocation of work_buffer
101 int buffer_size;
102 } quicktime_mpeg4_codec_t;
104 int ffmpeg_initialized = 0;
105 pthread_mutex_t ffmpeg_lock = PTHREAD_MUTEX_INITIALIZER;
108 // Decore needs the user to specify handles
109 static int decode_handle = 1;
110 static int encode_handle = 0;
120 // Utilities for programs wishing to parse MPEG-4
123 // Determine of the compressed frame is a keyframe for direct copy
124 int quicktime_mpeg4_is_key(unsigned char *data, long size, char *codec_id)
126 int result = 0;
127 int i;
129 if(quicktime_match_32(codec_id, QUICKTIME_DIVX) ||
130 quicktime_match_32(codec_id, QUICKTIME_HV60))
132 for(i = 0; i < size - 5; i++)
134 if( data[i] == 0x00 &&
135 data[i + 1] == 0x00 &&
136 data[i + 2] == 0x01 &&
137 data[i + 3] == 0xb6)
139 if((data[i + 4] & 0xc0) == 0x0)
140 return 1;
141 else
142 return 0;
146 return result;
150 // Test for VOL header in frame
151 int quicktime_mpeg4_has_vol(unsigned char *data)
153 if( data[0] == 0x00 &&
154 data[1] == 0x00 &&
155 data[2] == 0x01 &&
156 data[3] == 0x00 &&
157 data[4] == 0x00 &&
158 data[5] == 0x00 &&
159 data[6] == 0x01 &&
160 data[7] == 0x20)
161 return 1;
162 else
163 return 0;
169 static void putbits(unsigned char **data,
170 int *bit_pos,
171 uint64_t *bit_store,
172 int *total,
173 int count,
174 uint64_t value)
176 value &= 0xffffffffffffffffLL >> (64 - count);
178 while(64 - *bit_pos < count)
180 *(*data)++ = (*bit_store) >> 56;
181 (*bit_store) <<= 8;
182 (*bit_pos) -= 8;
185 (*bit_store) |= value << (64 - count - *bit_pos);
186 (*bit_pos) += count;
187 (*total) += count;
191 static void flushbits(unsigned char **data,
192 int *bit_pos,
193 uint64_t *bit_store)
195 //printf("flushbits %llx\n", (*bit_store));
196 while((*bit_pos) > 0)
198 *(*data)++ = (*bit_store) >> 56;
199 (*bit_store) <<= 8;
200 (*bit_pos) -= 8;
207 #define VO_START_CODE 0x8
208 #define VO_START_CODE_LENGTH 27
209 #define VOL_START_CODE 0x12 /* 25-MAR-97 JDL : according to WD2 */
210 #define VOL_START_CODE_LENGTH 28
214 int quicktime_mpeg4_write_vol(unsigned char *data_start,
215 int vol_width,
216 int vol_height,
217 int time_increment_resolution,
218 double frame_rate)
220 int written = 0;
221 int bits, fixed_vop_time_increment;
222 unsigned char *data = data_start;
223 int bit_pos;
224 uint64_t bit_store;
225 int i, j;
227 bit_store = 0;
228 bit_pos = 0;
229 vol_width = (int)((float)vol_width / 16 + 0.5) * 16;
230 vol_height = (int)((float)vol_height / 16 + 0.5) * 16;
233 putbits(&data,
234 &bit_pos,
235 &bit_store,
236 &written,
237 VO_START_CODE_LENGTH, VO_START_CODE);
238 putbits(&data,
239 &bit_pos,
240 &bit_store,
241 &written,
242 5, 0); /* vo_id = 0 */
244 putbits(&data,
245 &bit_pos,
246 &bit_store,
247 &written,
248 VOL_START_CODE_LENGTH, VOL_START_CODE);
253 putbits(&data,
254 &bit_pos,
255 &bit_store,
256 &written,
257 4, 0); /* vol_id = 0 */
259 putbits(&data,
260 &bit_pos,
261 &bit_store,
262 &written,
263 1, 0); /* random_accessible_vol = 0 */
264 putbits(&data,
265 &bit_pos,
266 &bit_store,
267 &written,
268 8, 1); /* video_object_type_indication = 1 video */
269 putbits(&data,
270 &bit_pos,
271 &bit_store,
272 &written,
273 1, 1); /* is_object_layer_identifier = 1 */
274 putbits(&data,
275 &bit_pos,
276 &bit_store,
277 &written,
278 4, 2); /* visual_object_layer_ver_id = 2 */
279 putbits(&data,
280 &bit_pos,
281 &bit_store,
282 &written,
283 3, 1); /* visual_object_layer_priority = 1 */
284 putbits(&data,
285 &bit_pos,
286 &bit_store,
287 &written,
288 4, 1); /* aspect_ratio_info = 1 */
296 putbits(&data,
297 &bit_pos,
298 &bit_store,
299 &written,
300 1, 0); /* vol_control_parameter = 0 */
301 putbits(&data,
302 &bit_pos,
303 &bit_store,
304 &written,
305 2, 0); /* vol_shape = 0 rectangular */
306 putbits(&data,
307 &bit_pos,
308 &bit_store,
309 &written,
310 1, 1); /* marker */
318 putbits(&data,
319 &bit_pos,
320 &bit_store,
321 &written,
322 16, time_increment_resolution);
323 putbits(&data,
324 &bit_pos,
325 &bit_store,
326 &written,
327 1, 1); /* marker */
328 putbits(&data,
329 &bit_pos,
330 &bit_store,
331 &written,
332 1, 1); /* fixed_vop_rate = 1 */
335 bits = 1;
336 while((1 << bits) < time_increment_resolution) bits++;
338 // Log calculation fails for some reason
339 // bits = (int)ceil(log((double)time_increment_resolution) / log(2.0));
340 // if (bits < 1) bits=1;
342 fixed_vop_time_increment =
343 (int)(time_increment_resolution / frame_rate + 0.1);
345 putbits(&data,
346 &bit_pos,
347 &bit_store,
348 &written,
349 bits, fixed_vop_time_increment);
351 putbits(&data,
352 &bit_pos,
353 &bit_store,
354 &written,
355 1, 1); /* marker */
357 putbits(&data,
358 &bit_pos,
359 &bit_store,
360 &written,
361 13, vol_width);
362 putbits(&data,
363 &bit_pos,
364 &bit_store,
365 &written,
366 1, 1); /* marker */
367 putbits(&data,
368 &bit_pos,
369 &bit_store,
370 &written,
371 13, vol_height);
372 putbits(&data,
373 &bit_pos,
374 &bit_store,
375 &written,
376 1, 1); /* marker */
378 putbits(&data,
379 &bit_pos,
380 &bit_store,
381 &written,
382 1, 0); /* interlaced = 0 */
383 putbits(&data,
384 &bit_pos,
385 &bit_store,
386 &written,
387 1, 1); /* OBMC_disabled = 1 */
388 putbits(&data,
389 &bit_pos,
390 &bit_store,
391 &written,
392 2, 0); /* vol_sprite_usage = 0 */
393 putbits(&data,
394 &bit_pos,
395 &bit_store,
396 &written,
397 1, 0); /* not_8_bit = 0 */
399 putbits(&data,
400 &bit_pos,
401 &bit_store,
402 &written,
403 1, 0); /* vol_quant_type = 0 */
404 putbits(&data,
405 &bit_pos,
406 &bit_store,
407 &written,
408 1, 0); /* vol_quarter_pixel = 0 */
409 putbits(&data,
410 &bit_pos,
411 &bit_store,
412 &written,
413 1, 1); /* complexity_estimation_disabled = 1 */
414 putbits(&data,
415 &bit_pos,
416 &bit_store,
417 &written,
418 1, 1); /* resync_marker_disabled = 1 */
419 putbits(&data,
420 &bit_pos,
421 &bit_store,
422 &written,
423 1, 0); /* data_partitioning_enabled = 0 */
424 putbits(&data,
425 &bit_pos,
426 &bit_store,
427 &written,
428 1, 0); /* scalability = 0 */
430 flushbits(&data,
431 &bit_pos,
432 &bit_store);
437 * for(i = 0; i < data - data_start; i++)
438 * for(j = 0x80; j >= 1; j /= 2)
439 * printf("%d", (data_start[i] & j) ? 1 : 0);
440 * printf("\n");
445 return data - data_start;
450 // Create the header for the esds block which is used in mp4v.
451 // Taken from libavcodec
452 // Returns the size
453 static int write_mp4v_header(unsigned char *data,
454 int w,
455 int h,
456 double frame_rate)
458 unsigned char *start = data;
459 // Advanced simple level 1
460 int profile_level = 0xf3;
461 int vo_version_id = 5;
467 // VOS startcode
468 *data++ = 0x00;
469 *data++ = 0x00;
470 *data++ = 0x01;
471 *data++ = 0xb0;
472 *data++ = profile_level;
474 // Visual object startcode
475 *data++ = 0x00;
476 *data++ = 0x00;
477 *data++ = 0x01;
478 *data++ = 0xb5;
479 *data++ = (1 << 7) ||
480 (vo_version_id << 3) ||
481 // Priority
483 // visual object type
484 *data++ = (1 << 4) ||
485 // Video signal type
487 *data++ = 0x40;
488 *data++ = 0xc0;
489 *data++ = 0xcf;
491 // video object
492 int vol_size = quicktime_mpeg4_write_vol(data,
495 60000,
496 frame_rate);
497 data += vol_size;
499 return data - start;
508 // Mpeg-4 interface
510 static int reads_colormodel(quicktime_t *file,
511 int colormodel,
512 int track)
514 quicktime_video_map_t *vtrack = &(file->vtracks[track]);
515 quicktime_codec_t *codec = (quicktime_codec_t*)vtrack->codec;
516 return (colormodel == BC_YUV420P &&
517 !quicktime_match_32(QUICKTIME_SVQ1, codec->fourcc));
520 static int writes_colormodel(quicktime_t *file,
521 int colormodel,
522 int track)
524 return (colormodel == BC_RGB888 ||
525 colormodel == BC_RGBA8888 ||
526 colormodel == BC_RGB161616 ||
527 colormodel == BC_RGBA16161616 ||
528 colormodel == BC_YUV888 ||
529 colormodel == BC_YUVA8888 ||
530 colormodel == BC_YUV161616 ||
531 colormodel == BC_YUVA16161616 ||
532 colormodel == BC_YUV420P ||
533 colormodel == BC_YUV422 ||
534 colormodel == BC_COMPRESSED);
538 static int delete_decoder(quicktime_mpeg4_codec_t *codec, int field)
540 if(codec->decode_initialized[field])
542 pthread_mutex_lock(&ffmpeg_lock);
544 avcodec_close(codec->decoder_context[field]);
545 free(codec->decoder_context[field]);
547 pthread_mutex_unlock(&ffmpeg_lock);
548 codec->decode_initialized[field] = 0;
553 static int init_decode(quicktime_mpeg4_codec_t *codec,
554 int current_field,
555 int width_i,
556 int height_i)
559 if(!ffmpeg_initialized)
561 ffmpeg_initialized = 1;
562 avcodec_init();
563 avcodec_register_all();
567 codec->decoder[current_field] = avcodec_find_decoder(codec->ffmpeg_id);
568 if(!codec->decoder[current_field])
570 printf("init_decode: avcodec_find_decoder returned NULL.\n");
571 return 1;
574 codec->decoder_context[current_field] = avcodec_alloc_context();
575 codec->decoder_context[current_field]->width = width_i;
576 codec->decoder_context[current_field]->height = height_i;
577 if(avcodec_open(codec->decoder_context[current_field],
578 codec->decoder[current_field]) < 0)
580 printf("init_decode: avcodec_open failed.\n");
582 return 0;
586 static int decode_wrapper(quicktime_t *file,
587 quicktime_video_map_t *vtrack,
588 quicktime_mpeg4_codec_t *codec,
589 int frame_number,
590 int current_field,
591 int track)
594 int got_picture = 0;
595 int result = 0;
596 int bytes = 0;
597 int header_bytes = 0;
598 char *compressor = vtrack->track->mdia.minf.stbl.stsd.table[0].format;
599 quicktime_trak_t *trak = vtrack->track;
600 quicktime_stsd_table_t *stsd_table = &trak->mdia.minf.stbl.stsd.table[0];
601 int width = trak->tkhd.track_width;
602 int height = trak->tkhd.track_height;
603 int width_i = (int)((float)width / 16 + 0.5) * 16;
604 int height_i = (int)((float)height / 16 + 0.5) * 16;
606 quicktime_set_video_position(file, frame_number, track);
608 bytes = quicktime_frame_size(file, frame_number, track);
609 if(frame_number == 0)
611 header_bytes = stsd_table->mpeg4_header_size;
613 //printf("decode_wrapper 1 %p %d %d %d\n", stsd_table, frame_number, bytes, header_bytes);
615 if(!codec->work_buffer || codec->buffer_size < bytes + header_bytes)
617 if(codec->work_buffer) free(codec->work_buffer);
618 codec->buffer_size = bytes + header_bytes;
619 codec->work_buffer = calloc(1, codec->buffer_size + 100);
622 if(header_bytes)
623 memcpy(codec->work_buffer, stsd_table->mpeg4_header, header_bytes);
625 //printf("decode_wrapper 1 %d %d\n", header_bytes, bytes);
626 if(!quicktime_read_data(file,
627 codec->work_buffer + header_bytes,
628 bytes))
629 result = -1;
632 if(!result)
635 * if(!codec->got_key[current_field])
637 * if(!quicktime_mpeg4_is_key(codec->work_buffer, bytes, compressor))
638 * return -1;
639 * else
641 * codec->got_key[current_field] = 1;
647 // No way to determine if there was an error based on nonzero status.
648 // Need to test row pointers to determine if an error occurred.
649 result = avcodec_decode_video(codec->decoder_context[current_field],
650 &codec->picture[current_field],
651 &got_picture,
652 codec->work_buffer,
653 bytes + header_bytes);
657 if(codec->picture[current_field].data[0])
659 if(!codec->got_key[current_field])
661 codec->got_key[current_field] = 1;
663 result = 0;
665 else
667 // ffmpeg can't recover if the first frame errored out, like in a direct copy
668 // sequence.
670 * delete_decoder(codec, current_field);
671 * init_decode(codec, current_field, width_i, height_i);
673 result = 1;
676 #ifdef ARCH_X86
677 asm("emms");
678 #endif
681 return result;
684 static int decode(quicktime_t *file, unsigned char **row_pointers, int track)
686 int i;
687 int result = 0;
688 quicktime_video_map_t *vtrack = &(file->vtracks[track]);
689 quicktime_trak_t *trak = vtrack->track;
690 quicktime_mpeg4_codec_t *codec = ((quicktime_codec_t*)vtrack->codec)->priv;
691 int width = trak->tkhd.track_width;
692 int height = trak->tkhd.track_height;
693 int width_i;
694 int height_i;
695 int use_temp = 0;
696 int input_cmodel;
697 int current_field = vtrack->current_position % codec->total_fields;
698 unsigned char **input_rows;
699 int seeking_done = 0;
701 if(codec->ffmpeg_id == CODEC_ID_SVQ1)
703 width_i = (int)((float)width / 16 + 0.5) * 32;
704 height_i = (int)((float)height / 16 + 0.5) * 32;
706 else
708 width_i = (int)((float)width / 16 + 0.5) * 16;
709 height_i = (int)((float)height / 16 + 0.5) * 16;
712 pthread_mutex_lock(&ffmpeg_lock);
715 if(!codec->decode_initialized[current_field])
717 int current_frame = vtrack->current_position;
718 init_decode(codec, current_field, width_i, height_i);
719 // Must decode frame with stream header first but only the first frame in the
720 // field sequence has a stream header.
721 result = decode_wrapper(file,
722 vtrack,
723 codec,
724 current_field,
725 current_field,
726 track);
727 // Reset position because decode wrapper set it
728 quicktime_set_video_position(file, current_frame, track);
729 codec->decode_initialized[current_field] = 1;
732 // Handle seeking
733 if(quicktime_has_keyframes(file, track) &&
734 vtrack->current_position != codec->last_frame[current_field] + codec->total_fields)
736 int frame1, frame2 = vtrack->current_position, current_frame = frame2;
737 int do_i_frame = 1;
738 //printf("decode 2\n");
740 // Get first keyframe of same field
743 frame1 = quicktime_get_keyframe_before(file,
744 current_frame--,
745 track);
746 }while(frame1 > 0 && (frame1 % codec->total_fields) != current_field);
747 //printf("decode 3\n");
749 // Keyframe is before last decoded frame and current frame is after last decoded
750 // frame, so instead of rerendering from the last keyframe we can rerender from
751 // the last decoded frame.
752 if(frame1 < codec->last_frame[current_field] &&
753 frame2 > codec->last_frame[current_field])
755 //printf("decode 1 %d %d\n", frame1, frame2);
756 frame1 = codec->last_frame[current_field] + codec->total_fields;
757 do_i_frame = 0;
760 while(frame1 <= frame2)
762 result = decode_wrapper(file,
763 vtrack,
764 codec,
765 frame1,
766 current_field,
767 track);
770 // May need to do the first I frame twice.
771 if(do_i_frame)
773 result = decode_wrapper(file,
774 vtrack,
775 codec,
776 frame1,
777 current_field,
778 track);
779 do_i_frame = 0;
781 frame1 += codec->total_fields;
784 vtrack->current_position = frame2;
785 seeking_done = 1;
789 if(!seeking_done)
791 result = decode_wrapper(file,
792 vtrack,
793 codec,
794 vtrack->current_position,
795 current_field,
796 track);
798 pthread_mutex_unlock(&ffmpeg_lock);
802 codec->last_frame[current_field] = vtrack->current_position;
812 // result = (result != 0);
813 switch(codec->decoder_context[current_field]->pix_fmt)
815 case PIX_FMT_YUV420P:
816 input_cmodel = BC_YUV420P;
817 break;
818 case PIX_FMT_YUV422:
819 input_cmodel = BC_YUV422;
820 break;
821 case PIX_FMT_YUV422P:
822 input_cmodel = BC_YUV422P;
823 break;
824 case PIX_FMT_YUV410P:
825 input_cmodel = BC_YUV9P;
826 break;
827 default:
828 fprintf(stderr,
829 "mpeg4 decode: unrecognized color model %d\n",
830 codec->decoder_context[current_field]->pix_fmt);
831 input_cmodel = BC_YUV420P;
832 break;
839 if(codec->picture[current_field].data[0])
842 input_rows =
843 malloc(sizeof(unsigned char*) *
844 codec->decoder_context[current_field]->height);
847 for(i = 0; i < codec->decoder_context[current_field]->height; i++)
848 input_rows[i] = codec->picture[current_field].data[0] +
849 i *
850 codec->decoder_context[current_field]->width *
851 cmodel_calculate_pixelsize(input_cmodel);
854 cmodel_transfer(row_pointers, /* Leave NULL if non existent */
855 input_rows,
856 row_pointers[0], /* Leave NULL if non existent */
857 row_pointers[1],
858 row_pointers[2],
859 codec->picture[current_field].data[0], /* Leave NULL if non existent */
860 codec->picture[current_field].data[1],
861 codec->picture[current_field].data[2],
862 file->in_x, /* Dimensions to capture from input frame */
863 file->in_y,
864 file->in_w,
865 file->in_h,
866 0, /* Dimensions to project on output frame */
868 file->out_w,
869 file->out_h,
870 input_cmodel,
871 file->color_model,
872 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
873 codec->picture[current_field].linesize[0], /* For planar use the luma rowspan */
874 width);
875 free(input_rows);
881 return result;
885 static int encode(quicktime_t *file, unsigned char **row_pointers, int track)
887 int64_t offset = quicktime_position(file);
888 quicktime_video_map_t *vtrack = &(file->vtracks[track]);
889 quicktime_mpeg4_codec_t *codec = ((quicktime_codec_t*)vtrack->codec)->priv;
890 quicktime_trak_t *trak = vtrack->track;
891 int width = trak->tkhd.track_width;
892 int height = trak->tkhd.track_height;
893 int width_i = (int)((float)width / 16 + 0.5) * 16;
894 int height_i = (int)((float)height / 16 + 0.5) * 16;
895 int result = 0;
896 int i;
897 int bytes = 0;
898 int is_keyframe = 0;
899 int current_field = vtrack->current_position % codec->total_fields;
900 quicktime_atom_t chunk_atom;
903 //printf("encode 1\n");
907 pthread_mutex_lock(&ffmpeg_lock);
911 if(!codec->encode_initialized[current_field])
913 // Encore section
914 if(codec->ffmpeg_id == CODEC_ID_MPEG4)
916 codec->use_encore = 1;
917 codec->encode_initialized[current_field] = 1;
918 codec->encode_handle[current_field] = encode_handle++;
919 codec->enc_param[current_field].x_dim = width_i;
920 codec->enc_param[current_field].y_dim = height_i;
921 codec->enc_param[current_field].framerate =
922 quicktime_frame_rate(file, track) / codec->total_fields;
923 codec->enc_param[current_field].bitrate =
924 codec->bitrate / codec->total_fields;
925 codec->enc_param[current_field].rc_period = codec->rc_period;
926 codec->enc_param[current_field].rc_reaction_period = codec->rc_reaction_period;
927 codec->enc_param[current_field].rc_reaction_ratio = codec->rc_reaction_ratio;
928 codec->enc_param[current_field].max_quantizer = codec->max_quantizer;
929 codec->enc_param[current_field].min_quantizer = codec->min_quantizer;
930 codec->enc_param[current_field].max_key_interval = codec->max_key_interval;
932 codec->enc_param[current_field].search_range = codec->quality * 3;
933 if(codec->enc_param[current_field].search_range > 15)
934 codec->enc_param[current_field].search_range = 15;
936 encore(codec->encode_handle[current_field],
937 ENC_OPT_INIT,
938 &codec->enc_param[current_field], NULL);
941 // Create esds header
942 if(!strcmp(((quicktime_codec_t*)vtrack->codec)->fourcc, QUICKTIME_MP4V))
944 unsigned char temp[256];
945 int size = write_mp4v_header(temp,
946 width,
947 height,
948 quicktime_frame_rate(file, track));
949 quicktime_set_mpeg4_header(&trak->mdia.minf.stbl.stsd.table[0],
950 temp,
951 size);
954 else
955 // ffmpeg section
957 static char *video_rc_eq="tex^qComp";
958 codec->encode_initialized[current_field] = 1;
959 if(!ffmpeg_initialized)
961 ffmpeg_initialized = 1;
962 avcodec_init();
963 avcodec_register_all();
966 codec->encoder[current_field] = avcodec_find_encoder(codec->ffmpeg_id);
967 if(!codec->encoder[current_field])
969 printf("encode: avcodec_find_encoder returned NULL.\n");
970 pthread_mutex_unlock(&ffmpeg_lock);
971 return 1;
974 codec->encoder_context[current_field] = avcodec_alloc_context();
975 codec->encoder_context[current_field]->frame_rate = FRAME_RATE_BASE *
976 quicktime_frame_rate(file, track);
977 codec->encoder_context[current_field]->width = width_i;
978 codec->encoder_context[current_field]->height = height_i;
979 codec->encoder_context[current_field]->gop_size = codec->gop_size;
980 codec->encoder_context[current_field]->pix_fmt = PIX_FMT_YUV420P;
981 codec->encoder_context[current_field]->bit_rate = codec->bitrate;
982 codec->encoder_context[current_field]->bit_rate_tolerance = codec->bitrate_tolerance;
983 codec->encoder_context[current_field]->rc_eq = video_rc_eq;
984 codec->encoder_context[current_field]->qmin = 2;
985 codec->encoder_context[current_field]->qmax = 31;
986 codec->encoder_context[current_field]->max_qdiff = 3;
987 codec->encoder_context[current_field]->qblur = 0.5;
988 codec->encoder_context[current_field]->qcompress = 0.5;
989 codec->encoder_context[current_field]->me_method = ME_FULL;
991 printf("encode %d %d %d %d %d\n", codec->gop_size, codec->bitrate, codec->bitrate_tolerance, codec->fix_bitrate, codec->interlaced);
994 if(!codec->fix_bitrate)
996 codec->encoder_context[current_field]->flags |= CODEC_FLAG_QSCALE;
999 if(codec->interlaced)
1001 codec->encoder_context[current_field]->flags |= CODEC_FLAG_INTERLACED_DCT;
1004 avcodec_open(codec->encoder_context[current_field], codec->encoder[current_field]);
1010 if(!codec->work_buffer)
1012 codec->buffer_size = width_i * height_i;
1013 codec->work_buffer = malloc(codec->buffer_size);
1018 // Encore section
1019 if(codec->use_encore)
1021 // Encore section
1022 ENC_FRAME encore_input;
1023 ENC_RESULT encore_result;
1026 // Assume planes are contiguous.
1027 // Encode directly from function arguments
1028 if(file->color_model == BC_YUV420P &&
1029 width == width_i &&
1030 height == height_i)
1032 encore_input.image = row_pointers[0];
1034 // Convert to YUV420P
1035 // Encode from temporary.
1036 else
1038 if(!codec->temp_frame)
1040 codec->temp_frame = malloc(width_i * height_i * 3 / 2);
1043 cmodel_transfer(0, /* Leave NULL if non existent */
1044 row_pointers,
1045 codec->temp_frame, /* Leave NULL if non existent */
1046 codec->temp_frame + width_i * height_i,
1047 codec->temp_frame + width_i * height_i + width_i * height_i / 4,
1048 row_pointers[0], /* Leave NULL if non existent */
1049 row_pointers[1],
1050 row_pointers[2],
1051 0, /* Dimensions to capture from input frame */
1053 width,
1054 height,
1055 0, /* Dimensions to project on output frame */
1057 width,
1058 height,
1059 file->color_model,
1060 BC_YUV420P,
1061 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
1062 width, /* For planar use the luma rowspan */
1063 width_i);
1066 encore_input.image = codec->temp_frame;
1071 bzero(codec->work_buffer, codec->buffer_size);
1072 encore_input.bitstream = codec->work_buffer;
1073 encore_input.length = 0;
1074 encore_input.quant = !codec->fix_bitrate ? codec->quantizer : 0;
1076 if(codec->p_count == 0)
1078 codec->p_count[current_field]++;
1080 else
1082 codec->p_count[current_field]++;
1083 if(codec->p_count[current_field] >= codec->max_key_interval)
1084 codec->p_count[current_field] = 0;
1087 encore(codec->encode_handle[current_field],
1089 &encore_input,
1090 &encore_result);
1092 bytes = encore_input.length;
1093 is_keyframe = encore_result.isKeyFrame;
1095 else
1096 // ffmpeg section
1098 AVFrame pict_tmp;
1100 if(width_i == width &&
1101 height_i == height &&
1102 file->color_model == BC_YUV420P)
1104 pict_tmp.data[0] = row_pointers[0];
1105 pict_tmp.data[1] = row_pointers[1];
1106 pict_tmp.data[2] = row_pointers[2];
1107 pict_tmp.linesize[0] = width_i;
1108 pict_tmp.linesize[1] = width_i / 2;
1109 pict_tmp.linesize[2] = width_i / 2;
1111 else
1113 if(!codec->temp_frame)
1115 codec->temp_frame = malloc(width_i * height_i * 3 / 2);
1118 cmodel_transfer(0, /* Leave NULL if non existent */
1119 row_pointers,
1120 codec->temp_frame, /* Leave NULL if non existent */
1121 codec->temp_frame + width_i * height_i,
1122 codec->temp_frame + width_i * height_i + width_i * height_i / 4,
1123 row_pointers[0], /* Leave NULL if non existent */
1124 row_pointers[1],
1125 row_pointers[2],
1126 0, /* Dimensions to capture from input frame */
1128 width,
1129 height,
1130 0, /* Dimensions to project on output frame */
1132 width,
1133 height,
1134 file->color_model,
1135 BC_YUV420P,
1136 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
1137 width, /* For planar use the luma rowspan */
1138 width_i);
1140 pict_tmp.data[0] = codec->temp_frame;
1141 pict_tmp.data[1] = codec->temp_frame + width_i * height_i;
1142 pict_tmp.data[2] = codec->temp_frame + width_i * height_i + width_i * height_i / 4;
1143 pict_tmp.linesize[0] = width_i;
1144 pict_tmp.linesize[1] = width_i / 2;
1145 pict_tmp.linesize[2] = width_i / 2;
1149 if(codec->quantizer >= 0)
1150 pict_tmp.quality = codec->quantizer;
1151 bytes = avcodec_encode_video(codec->encoder_context[current_field],
1152 codec->work_buffer,
1153 codec->buffer_size,
1154 &pict_tmp);
1155 is_keyframe = pict_tmp.key_frame;
1163 pthread_mutex_unlock(&ffmpeg_lock);
1164 quicktime_write_chunk_header(file, trak, &chunk_atom);
1165 result = !quicktime_write_data(file,
1166 codec->work_buffer,
1167 bytes);
1168 quicktime_write_chunk_footer(file,
1169 trak,
1170 vtrack->current_chunk,
1171 &chunk_atom,
1173 if(is_keyframe)
1174 quicktime_insert_keyframe(file,
1175 vtrack->current_position,
1176 track);
1177 //printf("encode 10\n");
1179 vtrack->current_chunk++;
1180 return result;
1189 static int set_parameter(quicktime_t *file,
1190 int track,
1191 char *key,
1192 void *value)
1194 quicktime_video_map_t *vtrack = &(file->vtracks[track]);
1195 char *compressor = vtrack->track->mdia.minf.stbl.stsd.table[0].format;
1197 if(quicktime_match_32(compressor, QUICKTIME_DIVX) ||
1198 quicktime_match_32(compressor, QUICKTIME_MP42) ||
1199 quicktime_match_32(compressor, QUICKTIME_MPG4) ||
1200 quicktime_match_32(compressor, QUICKTIME_DX50) ||
1201 quicktime_match_32(compressor, QUICKTIME_HV60))
1203 quicktime_mpeg4_codec_t *codec = ((quicktime_codec_t*)vtrack->codec)->priv;
1205 if(!strcasecmp(key, "divx_bitrate"))
1206 codec->bitrate = *(int*)value;
1207 else
1208 if(!strcasecmp(key, "divx_rc_period"))
1209 codec->rc_period = *(int*)value;
1210 else
1211 if(!strcasecmp(key, "divx_rc_reaction_ratio"))
1212 codec->rc_reaction_ratio = *(int*)value;
1213 else
1214 if(!strcasecmp(key, "divx_rc_reaction_period"))
1215 codec->rc_reaction_period = *(int*)value;
1216 else
1217 if(!strcasecmp(key, "divx_max_key_interval"))
1218 codec->max_key_interval = *(int*)value;
1219 else
1220 if(!strcasecmp(key, "divx_max_quantizer"))
1221 codec->max_quantizer = *(int*)value;
1222 else
1223 if(!strcasecmp(key, "divx_min_quantizer"))
1224 codec->min_quantizer = *(int*)value;
1225 else
1226 if(!strcasecmp(key, "divx_quantizer"))
1227 codec->quantizer = *(int*)value;
1228 else
1229 if(!strcasecmp(key, "divx_quality"))
1230 codec->quality = *(int*)value;
1231 else
1232 if(!strcasecmp(key, "divx_fix_bitrate"))
1233 codec->fix_bitrate = *(int*)value;
1234 else
1235 if(!strcasecmp(key, "divx_use_deblocking"))
1236 codec->use_deblocking = *(int*)value;
1238 else
1239 if(quicktime_match_32(compressor, QUICKTIME_DIV3))
1241 quicktime_mpeg4_codec_t *codec = ((quicktime_codec_t*)vtrack->codec)->priv;
1242 if(!strcasecmp(key, "div3_bitrate"))
1243 codec->bitrate = *(int*)value;
1244 else
1245 if(!strcasecmp(key, "div3_bitrate_tolerance"))
1246 codec->bitrate_tolerance = *(int*)value;
1247 else
1248 if(!strcasecmp(key, "div3_interlaced"))
1249 codec->interlaced = *(int*)value;
1250 else
1251 if(!strcasecmp(key, "div3_gop_size"))
1252 codec->gop_size = *(int*)value;
1253 else
1254 if(!strcasecmp(key, "div3_quantizer"))
1255 codec->quantizer = *(int*)value;
1256 else
1257 if(!strcasecmp(key, "div3_fix_bitrate"))
1258 codec->fix_bitrate = *(int*)value;
1260 return 0;
1265 static int delete_codec(quicktime_video_map_t *vtrack)
1267 quicktime_mpeg4_codec_t *codec;
1268 int i;
1271 codec = ((quicktime_codec_t*)vtrack->codec)->priv;
1272 for(i = 0; i < codec->total_fields; i++)
1274 if(codec->encode_initialized[i])
1276 pthread_mutex_lock(&ffmpeg_lock);
1277 if(codec->use_encore)
1279 encore(codec->encode_handle[i],
1280 ENC_OPT_RELEASE,
1284 else
1286 avcodec_close(codec->encoder_context[i]);
1287 free(codec->encoder_context[i]);
1289 pthread_mutex_unlock(&ffmpeg_lock);
1291 delete_decoder(codec, i);
1295 if(codec->temp_frame) free(codec->temp_frame);
1296 if(codec->work_buffer) free(codec->work_buffer);
1299 free(codec);
1300 return 0;
1306 static quicktime_mpeg4_codec_t* init_common(quicktime_video_map_t *vtrack,
1307 char *compressor,
1308 char *title,
1309 char *description)
1311 quicktime_codec_t *codec_base = (quicktime_codec_t*)vtrack->codec;
1312 quicktime_mpeg4_codec_t *codec;
1314 codec_base->priv = calloc(1, sizeof(quicktime_mpeg4_codec_t));
1315 codec_base->delete_vcodec = delete_codec;
1316 codec_base->decode_video = decode;
1317 codec_base->encode_video = encode;
1318 codec_base->reads_colormodel = reads_colormodel;
1319 codec_base->writes_colormodel = writes_colormodel;
1320 codec_base->set_parameter = set_parameter;
1321 codec_base->fourcc = compressor;
1322 codec_base->title = title;
1323 codec_base->desc = description;
1325 codec = (quicktime_mpeg4_codec_t*)codec_base->priv;
1329 // Set defaults
1330 codec->bitrate = 1000000;
1331 codec->rc_period = 50;
1332 codec->rc_reaction_ratio = 45;
1333 codec->rc_reaction_period = 10;
1334 codec->max_key_interval = 45;
1335 codec->max_quantizer = 31;
1336 codec->min_quantizer = 1;
1337 codec->quantizer = 10;
1338 codec->quality = 5;
1339 codec->fix_bitrate = 1;
1340 codec->total_fields = 1;
1344 return codec;
1350 // Mike Rowe Soft MPEG-4
1351 void quicktime_init_codec_div3(quicktime_video_map_t *vtrack)
1353 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1354 QUICKTIME_DIV3,
1355 "DIVX",
1356 "Mike Row Soft MPEG4 Version 3");
1357 result->ffmpeg_id = CODEC_ID_MSMPEG4V3;
1360 // Mike Rowe Soft MPEG-4
1361 void quicktime_init_codec_div3lower(quicktime_video_map_t *vtrack)
1363 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1364 QUICKTIME_DIV3_LOWER,
1365 "DIVX",
1366 "Mike Row Soft MPEG4 Version 3");
1367 result->ffmpeg_id = CODEC_ID_MSMPEG4V3;
1370 void quicktime_init_codec_div3v2(quicktime_video_map_t *vtrack)
1372 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1373 QUICKTIME_MP42,
1374 "MP42",
1375 "Mike Row Soft MPEG4 Version 2");
1376 result->ffmpeg_id = CODEC_ID_MSMPEG4V2;
1379 // Generic MPEG-4
1380 void quicktime_init_codec_divx(quicktime_video_map_t *vtrack)
1382 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1383 QUICKTIME_DIVX,
1384 "MPEG4",
1385 "Generic MPEG Four");
1386 result->ffmpeg_id = CODEC_ID_MPEG4;
1389 void quicktime_init_codec_mpg4(quicktime_video_map_t *vtrack)
1391 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1392 QUICKTIME_MPG4,
1393 "MPEG4",
1394 "FFMPEG (msmpeg4)");
1395 result->ffmpeg_id = CODEC_ID_MSMPEG4V1;
1398 void quicktime_init_codec_dx50(quicktime_video_map_t *vtrack)
1400 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1401 QUICKTIME_DX50,
1402 "MPEG4",
1403 "FFMPEG (mpeg4)");
1404 result->ffmpeg_id = CODEC_ID_MPEG4;
1407 // Generic MPEG-4
1408 void quicktime_init_codec_mp4v(quicktime_video_map_t *vtrack)
1410 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1411 QUICKTIME_MP4V,
1412 "MPEG4",
1413 "Generic MPEG Four");
1414 result->ffmpeg_id = CODEC_ID_MPEG4;
1418 // Mormon MPEG-4
1419 void quicktime_init_codec_svq1(quicktime_video_map_t *vtrack)
1421 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1422 QUICKTIME_SVQ1,
1423 "Sorenson Version 1",
1424 "From the chearch of codecs of yesterday's sights");
1425 result->ffmpeg_id = CODEC_ID_SVQ1;
1428 void quicktime_init_codec_svq3(quicktime_video_map_t *vtrack)
1430 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1431 QUICKTIME_SVQ3,
1432 "Sorenson Version 3",
1433 "From the chearch of codecs of yesterday's sights");
1434 result->ffmpeg_id = CODEC_ID_SVQ3;
1437 void quicktime_init_codec_h263(quicktime_video_map_t *vtrack)
1439 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1440 QUICKTIME_H263,
1441 "h263",
1442 "H.263");
1443 result->ffmpeg_id = CODEC_ID_H263;
1446 // field based MPEG-4
1447 void quicktime_init_codec_hv60(quicktime_video_map_t *vtrack)
1449 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1450 QUICKTIME_HV60,
1451 "Heroine 60",
1452 "MPEG 4 with alternating streams every other frame. (Not standardized)");
1453 result->total_fields = 2;
1454 result->ffmpeg_id = CODEC_ID_MPEG4;