r878: This is Cinelerra 2.1.
[cinelerra_cv/ct.git] / quicktime / mpeg4.c
bloba8af9f22c33764e9f41bd59c5e01ed5f089accf1
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 "avcodec.h"
11 #include "colormodels.h"
12 #include "funcprotos.h"
13 #include "qtffmpeg.h"
14 #include "quicktime.h"
15 #include "workarounds.h"
16 #include ENCORE_INCLUDE
17 //#include DECORE_INCLUDE
21 #include <pthread.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #define FRAME_RATE_BASE 10000
27 #define FIELDS 2
30 typedef struct
33 // Decoder side
34 quicktime_ffmpeg_t *decoder;
44 // Encoding side
45 int encode_initialized[FIELDS];
46 // Information for picking the right library routines.
47 // ID out of avcodec.h for the codec used.
48 // Invalid if encore50 is used.
49 int ffmpeg_id;
50 // True if encore50 is being used.
51 int use_encore;
53 // FFMpeg internals
54 AVCodec *encoder[FIELDS];
55 AVCodecContext *encoder_context[FIELDS];
56 AVFrame picture[FIELDS];
59 // Encore internals
60 int encode_handle[FIELDS];
61 ENC_PARAM enc_param[FIELDS];
62 // Must count pframes in VBR
63 int p_count[FIELDS];
66 // Encoding parameters
67 int bitrate;
68 // For heroine 60 encoding, we want different streams for each field.
69 int total_fields;
70 long rc_period; // the intended rate control averaging period
71 long rc_reaction_period; // the reation period for rate control
72 long rc_reaction_ratio; // the ratio for down/up rate control
73 long max_key_interval; // the maximum interval between key frames
74 int bitrate_tolerance;
75 int interlaced;
76 int gop_size;
77 int max_quantizer; // the upper limit of the quantizer
78 int min_quantizer; // the lower limit of the quantizer
79 int quantizer; // For vbr
80 int quality; // the forward search range for motion estimation
81 int fix_bitrate;
82 int use_deblocking;
85 // Temporary storage for color conversions
86 char *temp_frame;
87 // Storage of compressed data
88 unsigned char *work_buffer;
89 // Allocation of work_buffer
90 int buffer_size;
91 } quicktime_mpeg4_codec_t;
95 // Decore needs the user to specify handles
96 static int decode_handle = 1;
97 static int encode_handle = 0;
107 // Direct copy routines
110 // Determine of the compressed frame is a keyframe for direct copy
111 int quicktime_mpeg4_is_key(unsigned char *data, long size, char *codec_id)
113 int result = 0;
114 int i;
116 if(quicktime_match_32(codec_id, QUICKTIME_DIVX) ||
117 quicktime_match_32(codec_id, QUICKTIME_MP4V) ||
118 quicktime_match_32(codec_id, QUICKTIME_HV60))
120 for(i = 0; i < size - 5; i++)
122 if( data[i] == 0x00 &&
123 data[i + 1] == 0x00 &&
124 data[i + 2] == 0x01 &&
125 data[i + 3] == 0xb6)
127 if((data[i + 4] & 0xc0) == 0x0)
128 return 1;
129 else
130 return 0;
134 return result;
138 // Test for VOL header in frame
139 int quicktime_mpeg4_has_vol(unsigned char *data)
141 if( data[0] == 0x00 &&
142 data[1] == 0x00 &&
143 data[2] == 0x01 &&
144 data[3] == 0x00 &&
145 data[4] == 0x00 &&
146 data[5] == 0x00 &&
147 data[6] == 0x01 &&
148 data[7] == 0x20)
149 return 1;
150 else
151 return 0;
157 static void putbits(unsigned char **data,
158 int *bit_pos,
159 uint64_t *bit_store,
160 int *total,
161 int count,
162 uint64_t value)
164 value &= 0xffffffffffffffffLL >> (64 - count);
166 while(64 - *bit_pos < count)
168 *(*data)++ = (*bit_store) >> 56;
169 (*bit_store) <<= 8;
170 (*bit_pos) -= 8;
173 (*bit_store) |= value << (64 - count - *bit_pos);
174 (*bit_pos) += count;
175 (*total) += count;
179 static void flushbits(unsigned char **data,
180 int *bit_pos,
181 uint64_t *bit_store)
183 //printf("flushbits %llx\n", (*bit_store));
184 while((*bit_pos) > 0)
186 *(*data)++ = (*bit_store) >> 56;
187 (*bit_store) <<= 8;
188 (*bit_pos) -= 8;
195 #define VO_START_CODE 0x8
196 #define VO_START_CODE_LENGTH 27
197 #define VOL_START_CODE 0x12 /* 25-MAR-97 JDL : according to WD2 */
198 #define VOL_START_CODE_LENGTH 28
202 int quicktime_mpeg4_write_vol(unsigned char *data_start,
203 int vol_width,
204 int vol_height,
205 int time_increment_resolution,
206 double frame_rate)
208 int written = 0;
209 int bits, fixed_vop_time_increment;
210 unsigned char *data = data_start;
211 int bit_pos;
212 uint64_t bit_store;
213 int i, j;
215 bit_store = 0;
216 bit_pos = 0;
217 vol_width = quicktime_quantize16(vol_width);
218 vol_height = quicktime_quantize16(vol_height);
221 putbits(&data,
222 &bit_pos,
223 &bit_store,
224 &written,
225 VO_START_CODE_LENGTH, VO_START_CODE);
226 putbits(&data,
227 &bit_pos,
228 &bit_store,
229 &written,
230 5, 0); /* vo_id = 0 */
232 putbits(&data,
233 &bit_pos,
234 &bit_store,
235 &written,
236 VOL_START_CODE_LENGTH, VOL_START_CODE);
240 putbits(&data,
241 &bit_pos,
242 &bit_store,
243 &written,
244 4, 0); /* vol_id = 0 */
246 putbits(&data,
247 &bit_pos,
248 &bit_store,
249 &written,
250 1, 0); /* random_accessible_vol = 0 */
251 putbits(&data,
252 &bit_pos,
253 &bit_store,
254 &written,
255 8, 1); /* video_object_type_indication = 1 video */
256 putbits(&data,
257 &bit_pos,
258 &bit_store,
259 &written,
260 1, 1); /* is_object_layer_identifier = 1 */
261 putbits(&data,
262 &bit_pos,
263 &bit_store,
264 &written,
265 4, 2); /* visual_object_layer_ver_id = 2 */
266 putbits(&data,
267 &bit_pos,
268 &bit_store,
269 &written,
270 3, 1); /* visual_object_layer_priority = 1 */
271 putbits(&data,
272 &bit_pos,
273 &bit_store,
274 &written,
275 4, 1); /* aspect_ratio_info = 1 */
283 putbits(&data,
284 &bit_pos,
285 &bit_store,
286 &written,
287 1, 0); /* vol_control_parameter = 0 */
288 putbits(&data,
289 &bit_pos,
290 &bit_store,
291 &written,
292 2, 0); /* vol_shape = 0 rectangular */
293 putbits(&data,
294 &bit_pos,
295 &bit_store,
296 &written,
297 1, 1); /* marker */
305 putbits(&data,
306 &bit_pos,
307 &bit_store,
308 &written,
309 16, time_increment_resolution);
310 putbits(&data,
311 &bit_pos,
312 &bit_store,
313 &written,
314 1, 1); /* marker */
315 putbits(&data,
316 &bit_pos,
317 &bit_store,
318 &written,
319 1, 1); /* fixed_vop_rate = 1 */
322 bits = 1;
323 while((1 << bits) < time_increment_resolution) bits++;
325 // Log calculation fails for some reason
326 // bits = (int)ceil(log((double)time_increment_resolution) / log(2.0));
327 // if (bits < 1) bits=1;
329 fixed_vop_time_increment =
330 (int)(time_increment_resolution / frame_rate + 0.1);
332 putbits(&data,
333 &bit_pos,
334 &bit_store,
335 &written,
336 bits, fixed_vop_time_increment);
338 putbits(&data,
339 &bit_pos,
340 &bit_store,
341 &written,
342 1, 1); /* marker */
344 putbits(&data,
345 &bit_pos,
346 &bit_store,
347 &written,
348 13, vol_width);
349 putbits(&data,
350 &bit_pos,
351 &bit_store,
352 &written,
353 1, 1); /* marker */
354 putbits(&data,
355 &bit_pos,
356 &bit_store,
357 &written,
358 13, vol_height);
359 putbits(&data,
360 &bit_pos,
361 &bit_store,
362 &written,
363 1, 1); /* marker */
365 putbits(&data,
366 &bit_pos,
367 &bit_store,
368 &written,
369 1, 0); /* interlaced = 0 */
370 putbits(&data,
371 &bit_pos,
372 &bit_store,
373 &written,
374 1, 1); /* OBMC_disabled = 1 */
375 putbits(&data,
376 &bit_pos,
377 &bit_store,
378 &written,
379 2, 0); /* vol_sprite_usage = 0 */
380 putbits(&data,
381 &bit_pos,
382 &bit_store,
383 &written,
384 1, 0); /* not_8_bit = 0 */
386 putbits(&data,
387 &bit_pos,
388 &bit_store,
389 &written,
390 1, 0); /* vol_quant_type = 0 */
391 putbits(&data,
392 &bit_pos,
393 &bit_store,
394 &written,
395 1, 0); /* vol_quarter_pixel = 0 */
396 putbits(&data,
397 &bit_pos,
398 &bit_store,
399 &written,
400 1, 1); /* complexity_estimation_disabled = 1 */
401 putbits(&data,
402 &bit_pos,
403 &bit_store,
404 &written,
405 1, 1); /* resync_marker_disabled = 1 */
406 putbits(&data,
407 &bit_pos,
408 &bit_store,
409 &written,
410 1, 0); /* data_partitioning_enabled = 0 */
411 putbits(&data,
412 &bit_pos,
413 &bit_store,
414 &written,
415 1, 0); /* scalability = 0 */
417 flushbits(&data,
418 &bit_pos,
419 &bit_store);
423 * for(i = 0; i < data - data_start; i++)
424 * for(j = 0x80; j >= 1; j /= 2)
425 * printf("%d", (data_start[i] & j) ? 1 : 0);
426 * printf("\n");
431 return data - data_start;
436 // Create the header for the esds block which is used in mp4v.
437 // Taken from libavcodec
438 // Returns the size
439 static int write_mp4v_header(unsigned char *data,
440 int w,
441 int h,
442 double frame_rate)
444 unsigned char *start = data;
448 * static unsigned char test[] =
451 * 0x00, 0x00, 0x01, 0xb0, 0x01, 0x00, 0x00, 0x01, 0xb5, 0x89, 0x13,
452 * 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20, 0x00, 0xc4, 0x8d,
453 * 0x8a, 0xee, 0x05, 0x28, 0x04, 0x5a, 0x14, 0x63, 0x00, 0x00, 0x01, 0xb2,
454 * 0x46, 0x46, 0x6d, 0x70, 0x65, 0x67, 0x43, 0x56, 0x53, 0x62, 0x34, 0x37,
455 * 0x35, 0x38
457 * };
458 * memcpy(data, test, sizeof(test));
460 * return sizeof(test);
463 // From ffmpeg
464 // Advanced simple level 1
465 // int profile_level = 0xf3;
467 int profile_level = 0x1;
468 // int vo_version_id = 5;
469 int vo_version_id = 1;
475 // VOS startcode
476 *data++ = 0x00;
477 *data++ = 0x00;
478 *data++ = 0x01;
479 *data++ = 0xb0;
480 *data++ = profile_level;
482 // Visual object startcode
483 *data++ = 0x00;
484 *data++ = 0x00;
485 *data++ = 0x01;
486 *data++ = 0xb5;
487 *data++ = ((unsigned char)0x1 << 7) |
488 ((unsigned char)vo_version_id << 3) |
489 // Priority
490 (unsigned char)1;
491 // visual object type video
492 *data++ = (0x1 << 4) |
493 // Video signal type
494 (0 << 3) |
495 // Stuffing
496 0x3;
498 // *data++ = 0x40;
499 // *data++ = 0xc0;
500 // *data++ = 0xcf;
502 // video object
503 int vol_size = quicktime_mpeg4_write_vol(data,
506 60000,
507 frame_rate);
508 data += vol_size;
510 return data - start;
519 // Mpeg-4 interface
521 static int reads_colormodel(quicktime_t *file,
522 int colormodel,
523 int track)
525 quicktime_video_map_t *vtrack = &(file->vtracks[track]);
526 quicktime_codec_t *codec = (quicktime_codec_t*)vtrack->codec;
527 return (colormodel == BC_YUV420P &&
528 !quicktime_match_32(QUICKTIME_SVQ1, codec->fourcc));
531 static int writes_colormodel(quicktime_t *file,
532 int colormodel,
533 int track)
535 return colormodel == BC_YUV420P;
540 static int decode(quicktime_t *file, unsigned char **row_pointers, int track)
542 quicktime_video_map_t *vtrack = &(file->vtracks[track]);
543 quicktime_trak_t *trak = vtrack->track;
544 quicktime_mpeg4_codec_t *codec = ((quicktime_codec_t*)vtrack->codec)->priv;
545 quicktime_stsd_table_t *stsd_table = &trak->mdia.minf.stbl.stsd.table[0];
546 int width = trak->tkhd.track_width;
547 int height = trak->tkhd.track_height;
548 int result = 0;
551 if(!codec->decoder) codec->decoder = quicktime_new_ffmpeg(
552 file->cpus,
553 codec->total_fields,
554 codec->ffmpeg_id,
555 width,
556 height,
557 stsd_table);
559 if(codec->decoder) result = quicktime_ffmpeg_decode(
560 codec->decoder,
561 file,
562 row_pointers,
563 track);
566 return result;
570 static int encode(quicktime_t *file, unsigned char **row_pointers, int track)
572 int64_t offset = quicktime_position(file);
573 quicktime_video_map_t *vtrack = &(file->vtracks[track]);
574 quicktime_mpeg4_codec_t *codec = ((quicktime_codec_t*)vtrack->codec)->priv;
575 quicktime_trak_t *trak = vtrack->track;
576 int width = trak->tkhd.track_width;
577 int height = trak->tkhd.track_height;
578 int width_i = quicktime_quantize16(width);
579 int height_i = quicktime_quantize16(height);
580 int result = 0;
581 int i;
582 int bytes = 0;
583 int is_keyframe = 0;
584 int current_field = vtrack->current_position % codec->total_fields;
585 quicktime_atom_t chunk_atom;
591 pthread_mutex_lock(&ffmpeg_lock);
595 if(!codec->encode_initialized[current_field])
597 // Encore section
598 if(codec->ffmpeg_id == CODEC_ID_MPEG4 && codec->use_encore)
600 codec->encode_initialized[current_field] = 1;
601 codec->encode_handle[current_field] = encode_handle++;
602 codec->enc_param[current_field].x_dim = width_i;
603 codec->enc_param[current_field].y_dim = height_i;
604 codec->enc_param[current_field].framerate =
605 quicktime_frame_rate(file, track) / codec->total_fields;
606 codec->enc_param[current_field].bitrate =
607 codec->bitrate / codec->total_fields;
608 codec->enc_param[current_field].rc_period = codec->rc_period;
609 codec->enc_param[current_field].rc_reaction_period = codec->rc_reaction_period;
610 codec->enc_param[current_field].rc_reaction_ratio = codec->rc_reaction_ratio;
611 codec->enc_param[current_field].max_quantizer = codec->max_quantizer;
612 codec->enc_param[current_field].min_quantizer = codec->min_quantizer;
613 codec->enc_param[current_field].max_key_interval = codec->max_key_interval;
615 codec->enc_param[current_field].search_range = codec->quality * 3;
616 if(codec->enc_param[current_field].search_range > 15)
617 codec->enc_param[current_field].search_range = 15;
619 encore(codec->encode_handle[current_field],
620 ENC_OPT_INIT,
621 &codec->enc_param[current_field], NULL);
624 else
625 // ffmpeg section
627 static char *video_rc_eq="tex^qComp";
628 codec->encode_initialized[current_field] = 1;
629 if(!ffmpeg_initialized)
631 ffmpeg_initialized = 1;
632 avcodec_init();
633 avcodec_register_all();
636 codec->encoder[current_field] = avcodec_find_encoder(codec->ffmpeg_id);
637 if(!codec->encoder[current_field])
639 printf("encode: avcodec_find_encoder returned NULL.\n");
640 pthread_mutex_unlock(&ffmpeg_lock);
641 return 1;
644 codec->encoder_context[current_field] = avcodec_alloc_context();
645 AVCodecContext *context = codec->encoder_context[current_field];
647 context->width = width_i;
648 context->height = height_i;
649 context->gop_size = codec->gop_size;
650 context->pix_fmt = PIX_FMT_YUV420P;
651 context->bit_rate = codec->bitrate / codec->total_fields;
652 context->bit_rate_tolerance = codec->bitrate_tolerance;
653 context->rc_eq = video_rc_eq;
654 context->rc_max_rate = 0;
655 context->rc_min_rate = 0;
656 context->rc_buffer_size = 0;
657 context->qmin =
658 (!codec->fix_bitrate ? codec->quantizer : 2);
659 context->qmax =
660 (!codec->fix_bitrate ? codec->quantizer : 31);
661 context->lmin = 2 * FF_QP2LAMBDA;
662 context->lmax = 31 * FF_QP2LAMBDA;
663 context->mb_lmin = 2 * FF_QP2LAMBDA;
664 context->mb_lmax = 31 * FF_QP2LAMBDA;
665 context->max_qdiff = 3;
666 context->qblur = 0.5;
667 context->qcompress = 0.5;
668 // It needs the time per frame, not the frame rate.
669 context->time_base.den = quicktime_frame_rate_n(file, track);
670 context->time_base.num = quicktime_frame_rate_d(file, track);
672 context->b_quant_factor = 1.25;
673 context->b_quant_offset = 1.25;
674 context->error_resilience = FF_ER_CAREFULL;
675 context->error_concealment = 3;
676 context->frame_skip_cmp = FF_CMP_DCTMAX;
677 context->ildct_cmp = FF_CMP_VSAD;
678 context->intra_dc_precision = 0;
679 context->intra_quant_bias = FF_DEFAULT_QUANT_BIAS;
680 context->inter_quant_bias = FF_DEFAULT_QUANT_BIAS;
681 context->i_quant_factor = -0.8;
682 context->i_quant_offset = 0.0;
683 context->mb_decision = FF_MB_DECISION_SIMPLE;
684 context->mb_cmp = FF_CMP_SAD;
685 context->me_sub_cmp = FF_CMP_SAD;
686 context->me_cmp = FF_CMP_SAD;
687 context->me_pre_cmp = FF_CMP_SAD;
688 context->me_method = ME_EPZS;
689 context->me_subpel_quality = 8;
690 context->me_penalty_compensation = 256;
691 context->me_range = 0;
692 context->me_threshold = 0;
693 context->mb_threshold = 0;
694 context->nsse_weight= 8;
695 context->profile= FF_PROFILE_UNKNOWN;
696 context->rc_buffer_aggressivity = 1.0;
697 context->level= FF_LEVEL_UNKNOWN;
698 context->flags |= CODEC_FLAG_H263P_UMV;
699 context->flags |= CODEC_FLAG_AC_PRED;
700 context->flags |= CODEC_FLAG_4MV;
701 // Not compatible with Win
702 // context->flags |= CODEC_FLAG_QPEL;
704 if(file->cpus > 1)
706 avcodec_thread_init(context, file->cpus);
707 context->thread_count = file->cpus;
710 if(!codec->fix_bitrate)
711 context->flags |= CODEC_FLAG_QSCALE;
713 if(codec->interlaced)
715 context->flags |= CODEC_FLAG_INTERLACED_DCT;
716 context->flags |= CODEC_FLAG_INTERLACED_ME;
721 * printf("encode gop_size=%d fix_bitrate=%d quantizer=%d\n",
722 * codec->gop_size,
723 * codec->fix_bitrate,
724 * codec->quantizer);
726 avcodec_open(context, codec->encoder[current_field]);
728 avcodec_get_frame_defaults(&codec->picture[current_field]);
734 if(!codec->work_buffer)
736 codec->buffer_size = width_i * height_i;
737 codec->work_buffer = malloc(codec->buffer_size);
742 // Encore section
743 if(codec->use_encore)
745 // Encore section
746 ENC_FRAME encore_input;
747 ENC_RESULT encore_result;
750 // Assume planes are contiguous.
751 // Encode directly from function arguments
752 if(file->color_model == BC_YUV420P &&
753 width == width_i &&
754 height == height_i)
756 encore_input.image = row_pointers[0];
758 // Convert to YUV420P
759 // Encode from temporary.
760 else
762 if(!codec->temp_frame)
764 codec->temp_frame = malloc(width_i * height_i * 3 / 2);
767 cmodel_transfer(0, /* Leave NULL if non existent */
768 row_pointers,
769 codec->temp_frame, /* Leave NULL if non existent */
770 codec->temp_frame + width_i * height_i,
771 codec->temp_frame + width_i * height_i + width_i * height_i / 4,
772 row_pointers[0], /* Leave NULL if non existent */
773 row_pointers[1],
774 row_pointers[2],
775 0, /* Dimensions to capture from input frame */
777 width,
778 height,
779 0, /* Dimensions to project on output frame */
781 width,
782 height,
783 file->color_model,
784 BC_YUV420P,
785 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
786 width, /* For planar use the luma rowspan */
787 width_i);
790 encore_input.image = codec->temp_frame;
795 bzero(codec->work_buffer, codec->buffer_size);
796 encore_input.bitstream = codec->work_buffer;
797 encore_input.length = 0;
798 encore_input.quant = !codec->fix_bitrate ? codec->quantizer : 0;
800 if(codec->p_count == 0)
802 codec->p_count[current_field]++;
804 else
806 codec->p_count[current_field]++;
807 if(codec->p_count[current_field] >= codec->max_key_interval)
808 codec->p_count[current_field] = 0;
812 encore(codec->encode_handle[current_field],
814 &encore_input,
815 &encore_result);
817 bytes = encore_input.length;
818 is_keyframe = encore_result.isKeyFrame;
820 else
821 // ffmpeg section
823 AVCodecContext *context = codec->encoder_context[current_field];
824 AVFrame *picture = &codec->picture[current_field];
826 if(width_i == width &&
827 height_i == height &&
828 file->color_model == BC_YUV420P)
830 picture->data[0] = row_pointers[0];
831 picture->data[1] = row_pointers[1];
832 picture->data[2] = row_pointers[2];
833 picture->linesize[0] = width_i;
834 picture->linesize[1] = width_i / 2;
835 picture->linesize[2] = width_i / 2;
837 else
839 if(!codec->temp_frame)
841 codec->temp_frame = malloc(width_i * height_i * 3 / 2);
844 cmodel_transfer(0, /* Leave NULL if non existent */
845 row_pointers,
846 codec->temp_frame, /* Leave NULL if non existent */
847 codec->temp_frame + width_i * height_i,
848 codec->temp_frame + width_i * height_i + width_i * height_i / 4,
849 row_pointers[0], /* Leave NULL if non existent */
850 row_pointers[1],
851 row_pointers[2],
852 0, /* Dimensions to capture from input frame */
854 width,
855 height,
856 0, /* Dimensions to project on output frame */
858 width,
859 height,
860 file->color_model,
861 BC_YUV420P,
862 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
863 width, /* For planar use the luma rowspan */
864 width_i);
866 picture->data[0] = codec->temp_frame;
867 picture->data[1] = codec->temp_frame + width_i * height_i;
868 picture->data[2] = codec->temp_frame + width_i * height_i + width_i * height_i / 4;
869 picture->linesize[0] = width_i;
870 picture->linesize[1] = width_i / 2;
871 picture->linesize[2] = width_i / 2;
875 picture->pict_type = 0;
876 picture->quality = 0;
877 picture->pts = vtrack->current_position * quicktime_frame_rate_d(file, track);
878 picture->key_frame = 0;
879 bytes = avcodec_encode_video(context,
880 codec->work_buffer,
881 codec->buffer_size,
882 picture);
883 is_keyframe = context->coded_frame && context->coded_frame->key_frame;
885 * printf("encode current_position=%d is_keyframe=%d\n",
886 * vtrack->current_position,
887 * is_keyframe);
890 if(!trak->mdia.minf.stbl.stsd.table[0].esds.mpeg4_header_size &&
891 !strcmp(((quicktime_codec_t*)vtrack->codec)->fourcc, QUICKTIME_MP4V))
893 unsigned char temp[1024];
894 unsigned char *ptr = temp;
895 for(i = 0; i < bytes - 4; i++)
897 if(!(codec->work_buffer[i] == 0x00 &&
898 codec->work_buffer[i + 1] == 0x00 &&
899 codec->work_buffer[i + 2] == 0x01 &&
900 codec->work_buffer[i + 3] == 0xb3))
902 *ptr++ = codec->work_buffer[i];
904 else
905 break;
907 quicktime_set_mpeg4_header(&trak->mdia.minf.stbl.stsd.table[0],
908 temp,
909 ptr - temp);
910 trak->mdia.minf.stbl.stsd.table[0].version = 0;
919 pthread_mutex_unlock(&ffmpeg_lock);
920 quicktime_write_chunk_header(file, trak, &chunk_atom);
921 result = !quicktime_write_data(file,
922 codec->work_buffer,
923 bytes);
924 quicktime_write_chunk_footer(file,
925 trak,
926 vtrack->current_chunk,
927 &chunk_atom,
929 if(is_keyframe || vtrack->current_position == 0)
930 quicktime_insert_keyframe(file,
931 vtrack->current_position,
932 track);
934 vtrack->current_chunk++;
935 return result;
943 static void flush(quicktime_t *file, int track)
945 quicktime_video_map_t *track_map = &(file->vtracks[track]);
946 quicktime_trak_t *trak = track_map->track;
947 quicktime_mpeg4_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
949 // Create header
950 if(!trak->mdia.minf.stbl.stsd.table[0].esds.mpeg4_header_size &&
951 !strcmp(((quicktime_codec_t*)track_map->codec)->fourcc, QUICKTIME_MP4V))
953 int width = trak->tkhd.track_width;
954 int height = trak->tkhd.track_height;
955 int width_i = quicktime_quantize16(width);
956 int height_i = quicktime_quantize16(height);
958 unsigned char temp[1024];
959 int size = write_mp4v_header(temp,
960 width_i,
961 height_i,
962 quicktime_frame_rate(file, track));
963 quicktime_set_mpeg4_header(&trak->mdia.minf.stbl.stsd.table[0],
964 temp,
965 size);
968 // Create udta
969 file->moov.udta.require = strdup("QuickTime 6.0 or greater");
970 file->moov.udta.require_len = strlen(file->moov.udta.require);
980 static int set_parameter(quicktime_t *file,
981 int track,
982 char *key,
983 void *value)
985 quicktime_video_map_t *vtrack = &(file->vtracks[track]);
986 char *compressor = vtrack->track->mdia.minf.stbl.stsd.table[0].format;
988 if(quicktime_match_32(compressor, QUICKTIME_DIVX) ||
989 quicktime_match_32(compressor, QUICKTIME_MP42) ||
990 quicktime_match_32(compressor, QUICKTIME_MPG4) ||
991 quicktime_match_32(compressor, QUICKTIME_DX50) ||
992 quicktime_match_32(compressor, QUICKTIME_HV60))
994 quicktime_mpeg4_codec_t *codec = ((quicktime_codec_t*)vtrack->codec)->priv;
996 if(!strcasecmp(key, "divx_bitrate"))
997 codec->bitrate = *(int*)value;
998 else
999 if(!strcasecmp(key, "divx_rc_period"))
1000 codec->rc_period = *(int*)value;
1001 else
1002 if(!strcasecmp(key, "divx_rc_reaction_ratio"))
1003 codec->rc_reaction_ratio = *(int*)value;
1004 else
1005 if(!strcasecmp(key, "divx_rc_reaction_period"))
1006 codec->rc_reaction_period = *(int*)value;
1007 else
1008 if(!strcasecmp(key, "divx_max_key_interval"))
1009 codec->max_key_interval = *(int*)value;
1010 else
1011 if(!strcasecmp(key, "divx_max_quantizer"))
1012 codec->max_quantizer = *(int*)value;
1013 else
1014 if(!strcasecmp(key, "divx_min_quantizer"))
1015 codec->min_quantizer = *(int*)value;
1016 else
1017 if(!strcasecmp(key, "divx_quantizer"))
1018 codec->quantizer = *(int*)value;
1019 else
1020 if(!strcasecmp(key, "divx_quality"))
1021 codec->quality = *(int*)value;
1022 else
1023 if(!strcasecmp(key, "divx_fix_bitrate"))
1024 codec->fix_bitrate = *(int*)value;
1025 else
1026 if(!strcasecmp(key, "divx_use_deblocking"))
1027 codec->use_deblocking = *(int*)value;
1029 else
1030 if(quicktime_match_32(compressor, QUICKTIME_DIV3) ||
1031 quicktime_match_32(compressor, QUICKTIME_MP4V))
1033 quicktime_mpeg4_codec_t *codec = ((quicktime_codec_t*)vtrack->codec)->priv;
1034 if(!strcasecmp(key, "ffmpeg_bitrate"))
1035 codec->bitrate = *(int*)value;
1036 else
1037 if(!strcasecmp(key, "ffmpeg_bitrate_tolerance"))
1038 codec->bitrate_tolerance = *(int*)value;
1039 else
1040 if(!strcasecmp(key, "ffmpeg_interlaced"))
1041 codec->interlaced = *(int*)value;
1042 else
1043 if(!strcasecmp(key, "ffmpeg_gop_size"))
1044 codec->gop_size = *(int*)value;
1045 else
1046 if(!strcasecmp(key, "ffmpeg_quantizer"))
1047 codec->quantizer = *(int*)value;
1048 else
1049 if(!strcasecmp(key, "ffmpeg_fix_bitrate"))
1050 codec->fix_bitrate = *(int*)value;
1052 return 0;
1057 static int delete_codec(quicktime_video_map_t *vtrack)
1059 quicktime_mpeg4_codec_t *codec;
1060 int i;
1063 codec = ((quicktime_codec_t*)vtrack->codec)->priv;
1064 for(i = 0; i < codec->total_fields; i++)
1066 if(codec->encode_initialized[i])
1068 pthread_mutex_lock(&ffmpeg_lock);
1069 if(codec->use_encore)
1071 encore(codec->encode_handle[i],
1072 ENC_OPT_RELEASE,
1076 else
1078 avcodec_close(codec->encoder_context[i]);
1079 free(codec->encoder_context[i]);
1081 pthread_mutex_unlock(&ffmpeg_lock);
1086 if(codec->temp_frame) free(codec->temp_frame);
1087 if(codec->work_buffer) free(codec->work_buffer);
1088 if(codec->decoder) quicktime_delete_ffmpeg(codec->decoder);
1090 free(codec);
1091 return 0;
1097 static quicktime_mpeg4_codec_t* init_common(quicktime_video_map_t *vtrack,
1098 char *compressor,
1099 char *title,
1100 char *description)
1102 quicktime_codec_t *codec_base = (quicktime_codec_t*)vtrack->codec;
1103 quicktime_mpeg4_codec_t *codec;
1105 codec_base->priv = calloc(1, sizeof(quicktime_mpeg4_codec_t));
1106 codec_base->delete_vcodec = delete_codec;
1107 codec_base->decode_video = decode;
1108 codec_base->encode_video = encode;
1109 codec_base->flush = flush;
1110 codec_base->reads_colormodel = reads_colormodel;
1111 codec_base->writes_colormodel = writes_colormodel;
1112 codec_base->set_parameter = set_parameter;
1113 codec_base->fourcc = compressor;
1114 codec_base->title = title;
1115 codec_base->desc = description;
1117 codec = (quicktime_mpeg4_codec_t*)codec_base->priv;
1121 // Set defaults
1122 codec->bitrate = 1000000;
1123 codec->rc_period = 50;
1124 codec->rc_reaction_ratio = 45;
1125 codec->rc_reaction_period = 10;
1126 codec->max_key_interval = 45;
1127 codec->max_quantizer = 31;
1128 codec->min_quantizer = 1;
1129 codec->quantizer = 10;
1130 codec->quality = 5;
1131 codec->fix_bitrate = 1;
1132 codec->total_fields = 1;
1136 return codec;
1142 // Mike Rowe Soft MPEG-4
1143 void quicktime_init_codec_div3(quicktime_video_map_t *vtrack)
1145 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1146 QUICKTIME_DIV3,
1147 "DIVX",
1148 "Mike Row Soft MPEG4 Version 3");
1149 result->ffmpeg_id = CODEC_ID_MSMPEG4V3;
1152 void quicktime_init_codec_div5(quicktime_video_map_t *vtrack)
1154 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1155 QUICKTIME_DX50,
1156 "DIVX",
1157 "Mike Row Soft MPEG4 Version 5");
1158 result->ffmpeg_id = CODEC_ID_MPEG4;
1161 // Mike Rowe Soft MPEG-4
1162 void quicktime_init_codec_div3lower(quicktime_video_map_t *vtrack)
1164 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1165 QUICKTIME_DIV3_LOWER,
1166 "DIVX",
1167 "Mike Row Soft MPEG4 Version 3");
1168 result->ffmpeg_id = CODEC_ID_MSMPEG4V3;
1171 void quicktime_init_codec_div3v2(quicktime_video_map_t *vtrack)
1173 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1174 QUICKTIME_MP42,
1175 "MP42",
1176 "Mike Row Soft MPEG4 Version 2");
1177 result->ffmpeg_id = CODEC_ID_MSMPEG4V2;
1180 // Generic MPEG-4
1181 void quicktime_init_codec_divx(quicktime_video_map_t *vtrack)
1183 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1184 QUICKTIME_DIVX,
1185 "MPEG-4",
1186 "Generic MPEG Four");
1187 result->ffmpeg_id = CODEC_ID_MPEG4;
1188 result->use_encore = 1;
1191 void quicktime_init_codec_mpg4(quicktime_video_map_t *vtrack)
1193 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1194 QUICKTIME_MPG4,
1195 "MPEG-4",
1196 "FFMPEG (msmpeg4)");
1197 result->ffmpeg_id = CODEC_ID_MSMPEG4V1;
1200 void quicktime_init_codec_dx50(quicktime_video_map_t *vtrack)
1202 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1203 QUICKTIME_DX50,
1204 "MPEG-4",
1205 "FFMPEG (mpeg4)");
1206 result->ffmpeg_id = CODEC_ID_MPEG4;
1209 // Generic MPEG-4
1210 void quicktime_init_codec_mp4v(quicktime_video_map_t *vtrack)
1212 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1213 QUICKTIME_MP4V,
1214 "MPEG4",
1215 "Generic MPEG Four");
1216 result->ffmpeg_id = CODEC_ID_MPEG4;
1217 // result->use_encore = 1;
1221 // Mormon MPEG-4
1222 void quicktime_init_codec_svq1(quicktime_video_map_t *vtrack)
1224 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1225 QUICKTIME_SVQ1,
1226 "Sorenson Version 1",
1227 "From the chearch of codecs of yesterday's sights");
1228 result->ffmpeg_id = CODEC_ID_SVQ1;
1231 void quicktime_init_codec_svq3(quicktime_video_map_t *vtrack)
1233 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1234 QUICKTIME_SVQ3,
1235 "Sorenson Version 3",
1236 "From the chearch of codecs of yesterday's sights");
1237 result->ffmpeg_id = CODEC_ID_SVQ3;
1240 void quicktime_init_codec_h263(quicktime_video_map_t *vtrack)
1242 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1243 QUICKTIME_H263,
1244 "H.263",
1245 "H.263");
1246 result->ffmpeg_id = CODEC_ID_H263;
1249 void quicktime_init_codec_xvid(quicktime_video_map_t *vtrack)
1251 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1252 QUICKTIME_XVID,
1253 "XVID",
1254 "FFmpeg MPEG-4");
1255 result->ffmpeg_id = CODEC_ID_MPEG4;
1258 // field based MPEG-4
1259 void quicktime_init_codec_hv60(quicktime_video_map_t *vtrack)
1261 quicktime_mpeg4_codec_t *result = init_common(vtrack,
1262 QUICKTIME_HV60,
1263 "Dual MPEG-4",
1264 "MPEG 4 with alternating streams every other frame. (Not standardized)");
1265 result->total_fields = 2;
1266 result->ffmpeg_id = CODEC_ID_MPEG4;