WinGui: Fix another instance of the Caliburn vs Json.net sillyness where objects...
[HandBrake.git] / libhb / muxavformat.c
blobd54200e1a66366731f9054756d2dc2e7072d6c3c
1 /* muxavformat.c
3 Copyright (c) 2003-2015 HandBrake Team
4 This file is part of the HandBrake source code
5 Homepage: <http://handbrake.fr/>.
6 It may be used under the terms of the GNU General Public License v2.
7 For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
8 */
10 #include <ogg/ogg.h>
11 #include "libavformat/avformat.h"
12 #include "libavutil/avstring.h"
13 #include "libavutil/intreadwrite.h"
15 #include "hb.h"
16 #include "lang.h"
18 struct hb_mux_data_s
20 enum
22 MUX_TYPE_VIDEO,
23 MUX_TYPE_AUDIO,
24 MUX_TYPE_SUBTITLE
25 } type;
27 AVStream *st;
29 int64_t duration;
31 hb_buffer_t * delay_buf;
33 int64_t prev_chapter_tc;
34 int16_t current_chapter;
36 AVBitStreamFilterContext* bitstream_filter;
39 struct hb_mux_object_s
41 HB_MUX_COMMON;
43 hb_job_t * job;
45 AVFormatContext * oc;
46 AVRational time_base;
48 int ntracks;
49 hb_mux_data_t ** tracks;
51 int64_t chapter_delay;
54 enum
56 META_TITLE,
57 META_ARTIST,
58 META_DIRECTOR,
59 META_COMPOSER,
60 META_RELEASE_DATE,
61 META_COMMENT,
62 META_ALBUM,
63 META_GENRE,
64 META_DESCRIPTION,
65 META_SYNOPSIS,
66 META_LAST
69 enum
71 META_MUX_MP4,
72 META_MUX_MKV,
73 META_MUX_LAST
76 const char *metadata_keys[META_LAST][META_MUX_LAST] =
78 {"title", "TITLE"},
79 {"artist", "ARTIST"},
80 {"album_artist", "DIRECTOR"},
81 {"composer", "COMPOSER"},
82 {"date", "DATE_RELEASED"},
83 {"comment", "SUMMARY"},
84 {"album", NULL},
85 {"genre", "GENRE"},
86 {"description", "DESCRIPTION"},
87 {"synopsis", "SYNOPSIS"}
90 static char* lookup_lang_code(int mux, char *iso639_2)
92 iso639_lang_t *lang;
93 char *out = NULL;
95 switch (mux)
97 case HB_MUX_AV_MP4:
98 out = iso639_2;
99 break;
100 case HB_MUX_AV_MKV:
101 // MKV lang codes should be ISO-639-2B if it exists,
102 // else ISO-639-2
103 lang = lang_for_code2( iso639_2 );
104 out = lang->iso639_2b ? lang->iso639_2b : lang->iso639_2;
105 break;
106 default:
107 break;
109 return out;
112 /**********************************************************************
113 * avformatInit
114 **********************************************************************
115 * Allocates hb_mux_data_t structures, create file and write headers
116 *********************************************************************/
117 static int avformatInit( hb_mux_object_t * m )
119 hb_job_t * job = m->job;
120 hb_audio_t * audio;
121 hb_mux_data_t * track;
122 int meta_mux;
123 int max_tracks;
124 int ii, ret;
126 const char *muxer_name = NULL;
128 uint8_t default_track_flag = 1;
129 uint8_t need_fonts = 0;
130 char *lang;
133 max_tracks = 1 + hb_list_count( job->list_audio ) +
134 hb_list_count( job->list_subtitle );
136 m->tracks = calloc(max_tracks, sizeof(hb_mux_data_t*));
138 m->oc = avformat_alloc_context();
139 if (m->oc == NULL)
141 hb_error( "Could not initialize avformat context." );
142 goto error;
145 AVDictionary * av_opts = NULL;
146 switch (job->mux)
148 case HB_MUX_AV_MP4:
149 m->time_base.num = 1;
150 m->time_base.den = 90000;
151 if( job->ipod_atom )
152 muxer_name = "ipod";
153 else
154 muxer_name = "mp4";
155 meta_mux = META_MUX_MP4;
157 av_dict_set(&av_opts, "brand", "mp42", 0);
158 if (job->mp4_optimize)
159 av_dict_set(&av_opts, "movflags", "faststart+disable_chpl", 0);
160 else
161 av_dict_set(&av_opts, "movflags", "+disable_chpl", 0);
162 break;
164 case HB_MUX_AV_MKV:
165 // libavformat is essentially hard coded such that it only
166 // works with a timebase of 1/1000
167 m->time_base.num = 1;
168 m->time_base.den = 1000;
169 muxer_name = "matroska";
170 meta_mux = META_MUX_MKV;
171 break;
173 default:
175 hb_error("Invalid Mux %x", job->mux);
176 goto error;
179 m->oc->oformat = av_guess_format(muxer_name, NULL, NULL);
180 if(m->oc->oformat == NULL)
182 hb_error("Could not guess output format %s", muxer_name);
183 goto error;
185 av_strlcpy(m->oc->filename, job->file, sizeof(m->oc->filename));
186 ret = avio_open2(&m->oc->pb, job->file, AVIO_FLAG_WRITE,
187 &m->oc->interrupt_callback, NULL);
188 if( ret < 0 )
190 hb_error( "avio_open2 failed, errno %d", ret);
191 goto error;
194 /* Video track */
195 track = m->tracks[m->ntracks++] = calloc(1, sizeof( hb_mux_data_t ) );
196 job->mux_data = track;
198 track->type = MUX_TYPE_VIDEO;
199 track->prev_chapter_tc = AV_NOPTS_VALUE;
200 track->st = avformat_new_stream(m->oc, NULL);
201 if (track->st == NULL)
203 hb_error("Could not initialize video stream");
204 goto error;
206 track->st->time_base = m->time_base;
207 avcodec_get_context_defaults3(track->st->codec, NULL);
209 track->st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
210 track->st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
212 uint8_t *priv_data = NULL;
213 int priv_size = 0;
214 switch (job->vcodec)
216 case HB_VCODEC_X264:
217 case HB_VCODEC_QSV_H264:
218 track->st->codec->codec_id = AV_CODEC_ID_H264;
220 /* Taken from x264 muxers.c */
221 priv_size = 5 + 1 + 2 + job->config.h264.sps_length + 1 + 2 +
222 job->config.h264.pps_length;
223 priv_data = av_malloc(priv_size);
224 if (priv_data == NULL)
226 hb_error("H.264 extradata: malloc failure");
227 goto error;
230 priv_data[0] = 1;
231 priv_data[1] = job->config.h264.sps[1]; /* AVCProfileIndication */
232 priv_data[2] = job->config.h264.sps[2]; /* profile_compat */
233 priv_data[3] = job->config.h264.sps[3]; /* AVCLevelIndication */
234 priv_data[4] = 0xff; // nalu size length is four bytes
235 priv_data[5] = 0xe1; // one sps
237 priv_data[6] = job->config.h264.sps_length >> 8;
238 priv_data[7] = job->config.h264.sps_length;
240 memcpy(priv_data+8, job->config.h264.sps,
241 job->config.h264.sps_length);
243 priv_data[8+job->config.h264.sps_length] = 1; // one pps
244 priv_data[9+job->config.h264.sps_length] =
245 job->config.h264.pps_length >> 8;
246 priv_data[10+job->config.h264.sps_length] =
247 job->config.h264.pps_length;
249 memcpy(priv_data+11+job->config.h264.sps_length,
250 job->config.h264.pps, job->config.h264.pps_length );
251 break;
253 case HB_VCODEC_FFMPEG_MPEG4:
254 track->st->codec->codec_id = AV_CODEC_ID_MPEG4;
256 if (job->config.mpeg4.length != 0)
258 priv_size = job->config.mpeg4.length;
259 priv_data = av_malloc(priv_size);
260 if (priv_data == NULL)
262 hb_error("MPEG4 extradata: malloc failure");
263 goto error;
265 memcpy(priv_data, job->config.mpeg4.bytes, priv_size);
267 break;
269 case HB_VCODEC_FFMPEG_MPEG2:
270 track->st->codec->codec_id = AV_CODEC_ID_MPEG2VIDEO;
272 if (job->config.mpeg4.length != 0)
274 priv_size = job->config.mpeg4.length;
275 priv_data = av_malloc(priv_size);
276 if (priv_data == NULL)
278 hb_error("MPEG2 extradata: malloc failure");
279 goto error;
281 memcpy(priv_data, job->config.mpeg4.bytes, priv_size);
283 break;
285 case HB_VCODEC_FFMPEG_VP8:
286 track->st->codec->codec_id = AV_CODEC_ID_VP8;
287 priv_data = NULL;
288 priv_size = 0;
289 break;
291 case HB_VCODEC_THEORA:
293 track->st->codec->codec_id = AV_CODEC_ID_THEORA;
295 int size = 0;
296 ogg_packet *ogg_headers[3];
298 for (ii = 0; ii < 3; ii++)
300 ogg_headers[ii] = (ogg_packet *)job->config.theora.headers[ii];
301 size += ogg_headers[ii]->bytes + 2;
304 priv_size = size;
305 priv_data = av_malloc(priv_size);
306 if (priv_data == NULL)
308 hb_error("Theora extradata: malloc failure");
309 goto error;
312 size = 0;
313 for(ii = 0; ii < 3; ii++)
315 AV_WB16(priv_data + size, ogg_headers[ii]->bytes);
316 size += 2;
317 memcpy(priv_data+size, ogg_headers[ii]->packet,
318 ogg_headers[ii]->bytes);
319 size += ogg_headers[ii]->bytes;
321 } break;
323 case HB_VCODEC_X265:
324 track->st->codec->codec_id = AV_CODEC_ID_HEVC;
326 if (job->config.h265.headers_length > 0)
328 priv_size = job->config.h265.headers_length;
329 priv_data = av_malloc(priv_size);
330 if (priv_data == NULL)
332 hb_error("H.265 extradata: malloc failure");
333 goto error;
335 memcpy(priv_data, job->config.h265.headers, priv_size);
337 break;
339 default:
340 hb_error("muxavformat: Unknown video codec: %x", job->vcodec);
341 goto error;
343 track->st->codec->extradata = priv_data;
344 track->st->codec->extradata_size = priv_size;
346 track->st->sample_aspect_ratio.num = job->par.num;
347 track->st->sample_aspect_ratio.den = job->par.den;
348 track->st->codec->sample_aspect_ratio.num = job->par.num;
349 track->st->codec->sample_aspect_ratio.den = job->par.den;
350 track->st->codec->width = job->width;
351 track->st->codec->height = job->height;
352 track->st->disposition |= AV_DISPOSITION_DEFAULT;
354 hb_rational_t vrate;
355 if( job->pass_id == HB_PASS_ENCODE_2ND )
357 hb_interjob_t * interjob = hb_interjob_get( job->h );
358 vrate = interjob->vrate;
360 else
362 vrate = job->vrate;
365 // If the vrate is 27000000, there's a good chance this is
366 // a standard rate that we have in our hb_video_rates table.
367 // Because of rounding errors and approximations made while
368 // measuring framerate, the actual value may not be exact. So
369 // we look for rates that are "close" and make an adjustment
370 // to fps.den.
371 if (vrate.num == 27000000)
373 const hb_rate_t *video_framerate = NULL;
374 while ((video_framerate = hb_video_framerate_get_next(video_framerate)) != NULL)
376 if (abs(vrate.den - video_framerate->rate) < 10)
378 vrate.den = video_framerate->rate;
379 break;
383 hb_reduce(&vrate.num, &vrate.den, vrate.num, vrate.den);
384 if (job->mux == HB_MUX_AV_MP4)
386 // libavformat mp4 muxer requires that the codec time_base have the
387 // same denominator as the stream time_base, it uses it for the
388 // mdhd timescale.
389 double scale = (double)track->st->time_base.den / vrate.num;
390 track->st->codec->time_base.den = track->st->time_base.den;
391 track->st->codec->time_base.num = vrate.den * scale;
393 else
395 track->st->codec->time_base.num = vrate.den;
396 track->st->codec->time_base.den = vrate.num;
398 track->st->avg_frame_rate.num = vrate.num;
399 track->st->avg_frame_rate.den = vrate.den;
401 /* add the audio tracks */
402 for(ii = 0; ii < hb_list_count( job->list_audio ); ii++ )
404 audio = hb_list_item( job->list_audio, ii );
405 track = m->tracks[m->ntracks++] = calloc(1, sizeof( hb_mux_data_t ) );
406 audio->priv.mux_data = track;
408 track->type = MUX_TYPE_AUDIO;
410 track->st = avformat_new_stream(m->oc, NULL);
411 if (track->st == NULL)
413 hb_error("Could not initialize audio stream");
414 goto error;
416 avcodec_get_context_defaults3(track->st->codec, NULL);
418 track->st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
419 track->st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
420 if (job->mux == HB_MUX_AV_MP4)
422 track->st->codec->time_base.num = audio->config.out.samples_per_frame;
423 track->st->codec->time_base.den = audio->config.out.samplerate;
424 track->st->time_base.num = 1;
425 track->st->time_base.den = audio->config.out.samplerate;
427 else
429 track->st->codec->time_base = m->time_base;
430 track->st->time_base = m->time_base;
433 priv_data = NULL;
434 priv_size = 0;
435 switch (audio->config.out.codec & HB_ACODEC_MASK)
437 case HB_ACODEC_DCA:
438 case HB_ACODEC_DCA_HD:
439 track->st->codec->codec_id = AV_CODEC_ID_DTS;
440 break;
441 case HB_ACODEC_AC3:
442 track->st->codec->codec_id = AV_CODEC_ID_AC3;
443 break;
444 case HB_ACODEC_FFEAC3:
445 track->st->codec->codec_id = AV_CODEC_ID_EAC3;
446 break;
447 case HB_ACODEC_FFTRUEHD:
448 track->st->codec->codec_id = AV_CODEC_ID_TRUEHD;
449 break;
450 case HB_ACODEC_LAME:
451 case HB_ACODEC_MP3:
452 track->st->codec->codec_id = AV_CODEC_ID_MP3;
453 break;
454 case HB_ACODEC_VORBIS:
456 track->st->codec->codec_id = AV_CODEC_ID_VORBIS;
458 int jj, size = 0;
459 ogg_packet *ogg_headers[3];
461 for (jj = 0; jj < 3; jj++)
463 ogg_headers[jj] = (ogg_packet *)audio->priv.config.vorbis.headers[jj];
464 size += ogg_headers[jj]->bytes + 2;
467 priv_size = size;
468 priv_data = av_malloc(priv_size);
469 if (priv_data == NULL)
471 hb_error("Vorbis extradata: malloc failure");
472 goto error;
475 size = 0;
476 for(jj = 0; jj < 3; jj++)
478 AV_WB16(priv_data + size, ogg_headers[jj]->bytes);
479 size += 2;
480 memcpy(priv_data+size, ogg_headers[jj]->packet,
481 ogg_headers[jj]->bytes);
482 size += ogg_headers[jj]->bytes;
484 } break;
485 case HB_ACODEC_FFFLAC:
486 case HB_ACODEC_FFFLAC24:
487 track->st->codec->codec_id = AV_CODEC_ID_FLAC;
489 if (audio->priv.config.extradata.length)
491 priv_size = audio->priv.config.extradata.length;
492 priv_data = av_malloc(priv_size);
493 if (priv_data == NULL)
495 hb_error("FLAC extradata: malloc failure");
496 goto error;
498 memcpy(priv_data,
499 audio->priv.config.extradata.bytes,
500 audio->priv.config.extradata.length);
502 break;
503 case HB_ACODEC_FFAAC:
504 case HB_ACODEC_CA_AAC:
505 case HB_ACODEC_CA_HAAC:
506 case HB_ACODEC_FDK_AAC:
507 case HB_ACODEC_FDK_HAAC:
508 track->st->codec->codec_id = AV_CODEC_ID_AAC;
510 // libav mkv muxer expects there to be extradata for
511 // AAC and will crash if it is NULL. So allocate extra
512 // byte so that av_malloc does not return NULL when length
513 // is 0.
514 priv_size = audio->priv.config.extradata.length;
515 priv_data = av_malloc(priv_size + 1);
516 if (priv_data == NULL)
518 hb_error("AAC extradata: malloc failure");
519 goto error;
521 memcpy(priv_data,
522 audio->priv.config.extradata.bytes,
523 audio->priv.config.extradata.length);
525 // AAC from pass-through source may be ADTS.
526 // Therefore inserting "aac_adtstoasc" bitstream filter is
527 // preferred.
528 // The filter does nothing for non-ADTS bitstream.
529 if (audio->config.out.codec == HB_ACODEC_AAC_PASS)
531 track->bitstream_filter = av_bitstream_filter_init("aac_adtstoasc");
533 break;
534 default:
535 hb_error("muxavformat: Unknown audio codec: %x",
536 audio->config.out.codec);
537 goto error;
539 track->st->codec->extradata = priv_data;
540 track->st->codec->extradata_size = priv_size;
542 if( default_track_flag )
544 track->st->disposition |= AV_DISPOSITION_DEFAULT;
545 default_track_flag = 0;
548 lang = lookup_lang_code(job->mux, audio->config.lang.iso639_2 );
549 if (lang != NULL)
551 av_dict_set(&track->st->metadata, "language", lang, 0);
553 track->st->codec->sample_rate = audio->config.out.samplerate;
554 if (audio->config.out.codec & HB_ACODEC_PASS_FLAG)
556 track->st->codec->channels = av_get_channel_layout_nb_channels(audio->config.in.channel_layout);
557 track->st->codec->channel_layout = audio->config.in.channel_layout;
559 else
561 track->st->codec->channels = hb_mixdown_get_discrete_channel_count(audio->config.out.mixdown);
562 track->st->codec->channel_layout = hb_ff_mixdown_xlat(audio->config.out.mixdown, NULL);
565 char *name;
566 if (audio->config.out.name == NULL)
568 switch (track->st->codec->channels)
570 case 1:
571 name = "Mono";
572 break;
574 case 2:
575 name = "Stereo";
576 break;
578 default:
579 name = "Surround";
580 break;
583 else
585 name = audio->config.out.name;
587 // Set audio track title
588 av_dict_set(&track->st->metadata, "title", name, 0);
589 if (job->mux == HB_MUX_AV_MP4)
591 // Some software (MPC, mediainfo) use hdlr description
592 // for track title
593 av_dict_set(&track->st->metadata, "handler", name, 0);
597 char * subidx_fmt =
598 "size: %dx%d\n"
599 "org: %d, %d\n"
600 "scale: 100%%, 100%%\n"
601 "alpha: 100%%\n"
602 "smooth: OFF\n"
603 "fadein/out: 50, 50\n"
604 "align: OFF at LEFT TOP\n"
605 "time offset: 0\n"
606 "forced subs: %s\n"
607 "palette: %06x, %06x, %06x, %06x, %06x, %06x, "
608 "%06x, %06x, %06x, %06x, %06x, %06x, %06x, %06x, %06x, %06x\n"
609 "custom colors: OFF, tridx: 0000, "
610 "colors: 000000, 000000, 000000, 000000\n";
612 int subtitle_default = -1;
613 for( ii = 0; ii < hb_list_count( job->list_subtitle ); ii++ )
615 hb_subtitle_t *subtitle = hb_list_item( job->list_subtitle, ii );
617 if( subtitle->config.dest == PASSTHRUSUB )
619 if ( subtitle->config.default_track )
620 subtitle_default = ii;
623 // Quicktime requires that at least one subtitle is enabled,
624 // else it doesn't show any of the subtitles.
625 // So check to see if any of the subtitles are flagged to be
626 // the defualt. The default will the the enabled track, else
627 // enable the first track.
628 if (job->mux == HB_MUX_AV_MP4 && subtitle_default == -1)
630 subtitle_default = 0;
633 for( ii = 0; ii < hb_list_count( job->list_subtitle ); ii++ )
635 hb_subtitle_t * subtitle;
636 uint32_t rgb[16];
637 char subidx[2048];
638 int len;
640 subtitle = hb_list_item( job->list_subtitle, ii );
641 if (subtitle->config.dest != PASSTHRUSUB)
642 continue;
644 track = m->tracks[m->ntracks++] = calloc(1, sizeof( hb_mux_data_t ) );
645 subtitle->mux_data = track;
647 track->type = MUX_TYPE_SUBTITLE;
648 track->st = avformat_new_stream(m->oc, NULL);
649 if (track->st == NULL)
651 hb_error("Could not initialize subtitle stream");
652 goto error;
654 avcodec_get_context_defaults3(track->st->codec, NULL);
656 track->st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
657 track->st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
658 track->st->time_base = m->time_base;
659 track->st->codec->time_base = m->time_base;
660 track->st->codec->width = subtitle->width;
661 track->st->codec->height = subtitle->height;
663 priv_data = NULL;
664 priv_size = 0;
665 switch (subtitle->source)
667 case VOBSUB:
669 int jj;
670 track->st->codec->codec_id = AV_CODEC_ID_DVD_SUBTITLE;
672 for (jj = 0; jj < 16; jj++)
673 rgb[jj] = hb_yuv2rgb(subtitle->palette[jj]);
674 len = snprintf(subidx, 2048, subidx_fmt,
675 subtitle->width, subtitle->height,
676 0, 0, "OFF",
677 rgb[0], rgb[1], rgb[2], rgb[3],
678 rgb[4], rgb[5], rgb[6], rgb[7],
679 rgb[8], rgb[9], rgb[10], rgb[11],
680 rgb[12], rgb[13], rgb[14], rgb[15]);
682 priv_size = len + 1;
683 priv_data = av_malloc(priv_size);
684 if (priv_data == NULL)
686 hb_error("VOBSUB extradata: malloc failure");
687 goto error;
689 memcpy(priv_data, subidx, priv_size);
690 } break;
692 case PGSSUB:
694 track->st->codec->codec_id = AV_CODEC_ID_HDMV_PGS_SUBTITLE;
695 } break;
697 case CC608SUB:
698 case CC708SUB:
699 case TX3GSUB:
700 case SRTSUB:
701 case UTF8SUB:
702 case SSASUB:
704 if (job->mux == HB_MUX_AV_MP4)
706 track->st->codec->codec_id = AV_CODEC_ID_MOV_TEXT;
708 else
710 track->st->codec->codec_id = AV_CODEC_ID_SSA;
711 need_fonts = 1;
713 if (subtitle->extradata_size)
715 priv_size = subtitle->extradata_size;
716 priv_data = av_malloc(priv_size);
717 if (priv_data == NULL)
719 hb_error("SSA extradata: malloc failure");
720 goto error;
722 memcpy(priv_data, subtitle->extradata, priv_size);
725 } break;
727 default:
728 continue;
730 if (track->st->codec->codec_id == AV_CODEC_ID_MOV_TEXT)
732 // Build codec extradata for tx3g.
733 // If we were using a libav codec to generate this data
734 // this would (or should) be done for us.
735 uint8_t properties[] = {
736 0x00, 0x00, 0x00, 0x00, // Display Flags
737 0x01, // Horiz. Justification
738 0xff, // Vert. Justification
739 0x00, 0x00, 0x00, 0xff, // Bg color
740 0x00, 0x00, 0x00, 0x00, // Default text box
741 0x00, 0x00, 0x00, 0x00,
742 0x00, 0x00, 0x00, 0x00, // Reserved
743 0x00, 0x01, // Font ID
744 0x00, // Font face
745 0x18, // Font size
746 0xff, 0xff, 0xff, 0xff, // Fg color
747 // Font table:
748 0x00, 0x00, 0x00, 0x12, // Font table size
749 'f','t','a','b', // Tag
750 0x00, 0x01, // Count
751 0x00, 0x01, // Font ID
752 0x05, // Font name length
753 'A','r','i','a','l' // Font name
756 int width, height = 60;
757 width = job->width * job->par.num / job->par.den;
758 track->st->codec->width = width;
759 track->st->codec->height = height;
760 properties[14] = height >> 8;
761 properties[15] = height & 0xff;
762 properties[16] = width >> 8;
763 properties[17] = width & 0xff;
765 priv_size = sizeof(properties);
766 priv_data = av_malloc(priv_size);
767 if (priv_data == NULL)
769 hb_error("TX3G extradata: malloc failure");
770 goto error;
772 memcpy(priv_data, properties, priv_size);
774 track->st->codec->extradata = priv_data;
775 track->st->codec->extradata_size = priv_size;
777 if (ii == subtitle_default)
779 track->st->disposition |= AV_DISPOSITION_DEFAULT;
781 if (subtitle->config.default_track)
783 track->st->disposition |= AV_DISPOSITION_FORCED;
786 lang = lookup_lang_code(job->mux, subtitle->iso639_2 );
787 if (lang != NULL)
789 av_dict_set(&track->st->metadata, "language", lang, 0);
793 if (need_fonts)
795 hb_list_t * list_attachment = job->list_attachment;
796 int i;
797 for ( i = 0; i < hb_list_count(list_attachment); i++ )
799 hb_attachment_t * attachment = hb_list_item( list_attachment, i );
801 if ((attachment->type == FONT_TTF_ATTACH || attachment->type == FONT_OTF_ATTACH) &&
802 attachment->size > 0)
804 AVStream *st = avformat_new_stream(m->oc, NULL);
805 if (st == NULL)
807 hb_error("Could not initialize attachment stream");
808 goto error;
810 avcodec_get_context_defaults3(st->codec, NULL);
812 st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
813 if (attachment->type == FONT_TTF_ATTACH)
815 st->codec->codec_id = AV_CODEC_ID_TTF;
817 else if (attachment->type == FONT_OTF_ATTACH)
819 st->codec->codec_id = MKBETAG( 0 ,'O','T','F');
820 av_dict_set(&st->metadata, "mimetype", "application/vnd.ms-opentype", 0);
823 priv_size = attachment->size;
824 priv_data = av_malloc(priv_size);
825 if (priv_data == NULL)
827 hb_error("Font extradata: malloc failure");
828 goto error;
830 memcpy(priv_data, attachment->data, priv_size);
832 st->codec->extradata = priv_data;
833 st->codec->extradata_size = priv_size;
835 av_dict_set(&st->metadata, "filename", attachment->name, 0);
840 if( job->metadata )
842 hb_metadata_t *md = job->metadata;
844 hb_deep_log(2, "Writing Metadata to output file...");
845 if (md->name &&
846 metadata_keys[META_TITLE][meta_mux] != NULL)
848 av_dict_set(&m->oc->metadata,
849 metadata_keys[META_TITLE][meta_mux], md->name, 0);
851 if (md->artist &&
852 metadata_keys[META_ARTIST][meta_mux] != NULL)
854 av_dict_set(&m->oc->metadata,
855 metadata_keys[META_ARTIST][meta_mux], md->artist, 0);
857 if (md->album_artist &&
858 metadata_keys[META_DIRECTOR][meta_mux] != NULL)
860 av_dict_set(&m->oc->metadata,
861 metadata_keys[META_DIRECTOR][meta_mux],
862 md->album_artist, 0);
864 if (md->composer &&
865 metadata_keys[META_COMPOSER][meta_mux] != NULL)
867 av_dict_set(&m->oc->metadata,
868 metadata_keys[META_COMPOSER][meta_mux],
869 md->composer, 0);
871 if (md->release_date &&
872 metadata_keys[META_RELEASE_DATE][meta_mux] != NULL)
874 av_dict_set(&m->oc->metadata,
875 metadata_keys[META_RELEASE_DATE][meta_mux],
876 md->release_date, 0);
878 if (md->comment &&
879 metadata_keys[META_COMMENT][meta_mux] != NULL)
881 av_dict_set(&m->oc->metadata,
882 metadata_keys[META_COMMENT][meta_mux], md->comment, 0);
884 if (!md->name && md->album &&
885 metadata_keys[META_ALBUM][meta_mux] != NULL)
887 av_dict_set(&m->oc->metadata,
888 metadata_keys[META_ALBUM][meta_mux], md->album, 0);
890 if (md->genre &&
891 metadata_keys[META_GENRE][meta_mux] != NULL)
893 av_dict_set(&m->oc->metadata,
894 metadata_keys[META_GENRE][meta_mux], md->genre, 0);
896 if (md->description &&
897 metadata_keys[META_DESCRIPTION][meta_mux] != NULL)
899 av_dict_set(&m->oc->metadata,
900 metadata_keys[META_DESCRIPTION][meta_mux],
901 md->description, 0);
903 if (md->long_description &&
904 metadata_keys[META_SYNOPSIS][meta_mux] != NULL)
906 av_dict_set(&m->oc->metadata,
907 metadata_keys[META_SYNOPSIS][meta_mux],
908 md->long_description, 0);
912 char tool_string[80];
913 snprintf(tool_string, sizeof(tool_string), "HandBrake %s %i",
914 HB_PROJECT_VERSION, HB_PROJECT_BUILD);
915 av_dict_set(&m->oc->metadata, "encoding_tool", tool_string, 0);
916 time_t now = time(NULL);
917 struct tm * now_utc = gmtime(&now);
918 char now_8601[24];
919 strftime(now_8601, sizeof(now_8601), "%Y-%m-%dT%H:%M:%SZ", now_utc);
920 av_dict_set(&m->oc->metadata, "creation_time", now_8601, 0);
922 ret = avformat_write_header(m->oc, &av_opts);
923 if( ret < 0 )
925 av_dict_free( &av_opts );
926 hb_error( "muxavformat: avformat_write_header failed!");
927 goto error;
930 AVDictionaryEntry *t = NULL;
931 while( ( t = av_dict_get( av_opts, "", t, AV_DICT_IGNORE_SUFFIX ) ) )
933 hb_log( "muxavformat: Unknown option %s", t->key );
935 av_dict_free( &av_opts );
937 return 0;
939 error:
940 free(job->mux_data);
941 job->mux_data = NULL;
942 avformat_free_context(m->oc);
943 *job->done_error = HB_ERROR_INIT;
944 *job->die = 1;
945 return -1;
948 static int add_chapter(hb_mux_object_t *m, int64_t start, int64_t end, char * title)
950 AVChapter *chap;
951 AVChapter **chapters;
952 int nchap = m->oc->nb_chapters;
954 nchap++;
955 chapters = av_realloc(m->oc->chapters, nchap * sizeof(AVChapter*));
956 if (chapters == NULL)
958 hb_error("chapter array: malloc failure");
959 return -1;
962 chap = av_mallocz(sizeof(AVChapter));
963 if (chap == NULL)
965 hb_error("chapter: malloc failure");
966 return -1;
969 m->oc->chapters = chapters;
970 m->oc->chapters[nchap-1] = chap;
971 m->oc->nb_chapters = nchap;
973 chap->id = nchap;
974 chap->time_base = m->time_base;
975 // libav does not currently have a good way to deal with chapters and
976 // delayed stream timestamps. It makes no corrections to the chapter
977 // track. A patch to libav would touch a lot of things, so for now,
978 // work around the issue here.
979 chap->start = start + m->chapter_delay;
980 chap->end = end;
981 av_dict_set(&chap->metadata, "title", title, 0);
983 return 0;
986 static int avformatMux(hb_mux_object_t *m, hb_mux_data_t *track, hb_buffer_t *buf)
988 AVPacket pkt;
989 int64_t dts, pts, duration = AV_NOPTS_VALUE;
990 hb_job_t *job = m->job;
991 uint8_t sub_out[2048];
993 if (track->type == MUX_TYPE_VIDEO &&
994 track->prev_chapter_tc == AV_NOPTS_VALUE)
996 // Chapter timestamps are biased the same as video timestamps.
997 // This needs to be reflected in the initial chapter timestamp.
999 // TODO: Don't assume the first chapter is at 0. Pass the first
1000 // chapter through the pipeline instead of dropping it as we
1001 // currently do.
1002 m->chapter_delay = av_rescale_q(m->job->config.h264.init_delay,
1003 (AVRational){1,90000},
1004 track->st->time_base);
1005 track->prev_chapter_tc = -m->chapter_delay;
1007 // We only compute dts duration for MP4 files
1008 if (track->type == MUX_TYPE_VIDEO && (job->mux & HB_MUX_MASK_MP4))
1010 hb_buffer_t * tmp;
1012 // delay by one frame so that we can compute duration properly.
1013 tmp = track->delay_buf;
1014 track->delay_buf = buf;
1015 buf = tmp;
1017 if (buf == NULL)
1018 return 0;
1020 if (buf->s.renderOffset == AV_NOPTS_VALUE)
1022 dts = av_rescale_q(buf->s.start, (AVRational){1,90000},
1023 track->st->time_base);
1025 else
1027 dts = av_rescale_q(buf->s.renderOffset, (AVRational){1,90000},
1028 track->st->time_base);
1031 pts = av_rescale_q(buf->s.start, (AVRational){1,90000},
1032 track->st->time_base);
1034 if (track->type == MUX_TYPE_VIDEO && track->delay_buf != NULL)
1036 int64_t delayed_dts;
1037 delayed_dts = av_rescale_q(track->delay_buf->s.renderOffset,
1038 (AVRational){1,90000},
1039 track->st->time_base);
1040 duration = delayed_dts - dts;
1042 if (duration < 0 && buf->s.duration > 0)
1044 duration = av_rescale_q(buf->s.duration, (AVRational){1,90000},
1045 track->st->time_base);
1047 if (duration < 0)
1049 // There is a possiblility that some subtitles get through the pipeline
1050 // without ever discovering their true duration. Make the duration
1051 // 10 seconds in this case. Unless they are PGS subs which should
1052 // have zero duration.
1053 if (track->type == MUX_TYPE_SUBTITLE &&
1054 track->st->codec->codec_id != AV_CODEC_ID_HDMV_PGS_SUBTITLE)
1055 duration = av_rescale_q(10, (AVRational){1,1},
1056 track->st->time_base);
1057 else
1058 duration = 0;
1061 av_init_packet(&pkt);
1062 pkt.data = buf->data;
1063 pkt.size = buf->size;
1064 pkt.dts = dts;
1065 pkt.pts = pts;
1066 pkt.duration = duration;
1068 if (track->type == MUX_TYPE_VIDEO && ((job->vcodec & HB_VCODEC_H264_MASK) ||
1069 (job->vcodec & HB_VCODEC_FFMPEG_MASK)))
1071 if (buf->s.frametype == HB_FRAME_IDR)
1072 pkt.flags |= AV_PKT_FLAG_KEY;
1074 else if (buf->s.frametype & HB_FRAME_KEY)
1076 pkt.flags |= AV_PKT_FLAG_KEY;
1079 switch (track->type)
1081 case MUX_TYPE_VIDEO:
1083 if (job->chapter_markers && buf->s.new_chap)
1085 hb_chapter_t *chapter;
1087 // reached chapter N, write marker for chapter N-1
1088 // we don't know the end time of chapter N-1 till we receive
1089 // chapter N. So we are always writing the previous chapter
1090 // mark.
1091 track->current_chapter = buf->s.new_chap - 1;
1093 // chapter numbers start at 1, but the list starts at 0
1094 chapter = hb_list_item(job->list_chapter,
1095 track->current_chapter - 1);
1097 // make sure we're not writing a chapter that has 0 length
1098 if (chapter != NULL && track->prev_chapter_tc < pkt.pts)
1100 char title[1024];
1101 if (chapter->title != NULL)
1103 snprintf(title, 1023, "%s", chapter->title);
1105 else
1107 snprintf(title, 1023, "Chapter %d",
1108 track->current_chapter);
1110 add_chapter(m, track->prev_chapter_tc, pkt.pts, title);
1112 track->prev_chapter_tc = pkt.pts;
1114 } break;
1116 case MUX_TYPE_SUBTITLE:
1118 if (job->mux == HB_MUX_AV_MP4)
1120 /* Write an empty sample */
1121 if ( track->duration < pts )
1123 AVPacket empty_pkt;
1124 uint8_t empty[2] = {0,0};
1126 av_init_packet(&empty_pkt);
1127 empty_pkt.data = empty;
1128 empty_pkt.size = 2;
1129 empty_pkt.dts = track->duration;
1130 empty_pkt.pts = track->duration;
1131 empty_pkt.duration = pts - duration;
1132 empty_pkt.convergence_duration = empty_pkt.duration;
1133 empty_pkt.stream_index = track->st->index;
1134 int ret = av_interleaved_write_frame(m->oc, &empty_pkt);
1135 if (ret < 0)
1137 char errstr[64];
1138 av_strerror(ret, errstr, sizeof(errstr));
1139 hb_error("avformatMux: track %d, av_interleaved_write_frame failed with error '%s' (empty_pkt)",
1140 track->st->index, errstr);
1141 *job->done_error = HB_ERROR_UNKNOWN;
1142 *job->die = 1;
1143 return -1;
1146 if (track->st->codec->codec_id == AV_CODEC_ID_MOV_TEXT)
1148 uint8_t styleatom[2048];;
1149 uint16_t stylesize = 0;
1150 uint8_t buffer[2048];
1151 uint16_t buffersize = 0;
1153 *buffer = '\0';
1156 * Copy the subtitle into buffer stripping markup and creating
1157 * style atoms for them.
1159 hb_muxmp4_process_subtitle_style( buf->data,
1160 buffer,
1161 styleatom, &stylesize );
1163 buffersize = strlen((char*)buffer);
1165 /* Write the subtitle sample */
1166 memcpy( sub_out + 2, buffer, buffersize );
1167 memcpy( sub_out + 2 + buffersize, styleatom, stylesize);
1168 sub_out[0] = ( buffersize >> 8 ) & 0xff;
1169 sub_out[1] = buffersize & 0xff;
1170 pkt.data = sub_out;
1171 pkt.size = buffersize + stylesize + 2;
1174 if (track->st->codec->codec_id == AV_CODEC_ID_SSA &&
1175 job->mux == HB_MUX_AV_MKV)
1177 // avformat requires the this additional information
1178 // which it parses and then strips away
1179 int start_hh, start_mm, start_ss, start_ms;
1180 int stop_hh, stop_mm, stop_ss, stop_ms, layer;
1181 char *ssa;
1183 start_hh = buf->s.start / (90000 * 60 * 60);
1184 start_mm = (buf->s.start / (90000 * 60)) % 60;
1185 start_ss = (buf->s.start / 90000) % 60;
1186 start_ms = (buf->s.start / 900) % 100;
1187 stop_hh = buf->s.stop / (90000 * 60 * 60);
1188 stop_mm = (buf->s.stop / (90000 * 60)) % 60;
1189 stop_ss = (buf->s.stop / 90000) % 60;
1190 stop_ms = (buf->s.stop / 900) % 100;
1192 // Skip the read-order field
1193 ssa = strchr((char*)buf->data, ',');
1194 if (ssa != NULL)
1195 ssa++;
1196 // Skip the layer field
1197 layer = strtol(ssa, NULL, 10);
1198 ssa = strchr(ssa, ',');
1199 if (ssa != NULL)
1200 ssa++;
1201 sprintf((char*)sub_out,
1202 "Dialogue: %d,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s",
1203 layer,
1204 start_hh, start_mm, start_ss, start_ms,
1205 stop_hh, stop_mm, stop_ss, stop_ms, ssa);
1206 pkt.data = sub_out;
1207 pkt.size = strlen((char*)sub_out) + 1;
1209 pkt.convergence_duration = pkt.duration;
1211 } break;
1212 case MUX_TYPE_AUDIO:
1213 default:
1214 break;
1216 track->duration = pts + pkt.duration;
1218 if (track->bitstream_filter)
1220 av_bitstream_filter_filter(track->bitstream_filter, track->st->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
1223 pkt.stream_index = track->st->index;
1224 int ret = av_interleaved_write_frame(m->oc, &pkt);
1225 // Many avformat muxer functions do not check the error status
1226 // of the AVIOContext. So we need to check it ourselves to detect
1227 // write errors (like disk full condition).
1228 if (ret < 0 || m->oc->pb->error != 0)
1230 char errstr[64];
1231 av_strerror(ret < 0 ? ret : m->oc->pb->error, errstr, sizeof(errstr));
1232 hb_error("avformatMux: track %d, av_interleaved_write_frame failed with error '%s'",
1233 track->st->index, errstr);
1234 *job->done_error = HB_ERROR_UNKNOWN;
1235 *job->die = 1;
1236 return -1;
1239 hb_buffer_close( &buf );
1240 return 0;
1243 static int avformatEnd(hb_mux_object_t *m)
1245 hb_job_t *job = m->job;
1246 hb_mux_data_t *track = job->mux_data;
1248 if( !job->mux_data )
1251 * We must have failed to create the file in the first place.
1253 return 0;
1256 // Flush any delayed frames
1257 int ii;
1258 for (ii = 0; ii < m->ntracks; ii++)
1260 avformatMux(m, m->tracks[ii], NULL);
1262 if (m->tracks[ii]->bitstream_filter)
1264 av_bitstream_filter_close(m->tracks[ii]->bitstream_filter);
1268 if (job->chapter_markers)
1270 hb_chapter_t *chapter;
1272 // get the last chapter
1273 chapter = hb_list_item(job->list_chapter, track->current_chapter++);
1275 // only write the last chapter marker if it lasts at least 1.5 second
1276 if (chapter != NULL && chapter->duration > 135000LL)
1278 char title[1024];
1279 if (chapter->title != NULL)
1281 snprintf(title, 1023, "%s", chapter->title);
1283 else
1285 snprintf(title, 1023, "Chapter %d", track->current_chapter);
1287 add_chapter(m, track->prev_chapter_tc, track->duration, title);
1291 // Update and track private data that can change during
1292 // encode.
1293 for(ii = 0; ii < hb_list_count( job->list_audio ); ii++)
1295 AVStream *st;
1296 hb_audio_t * audio;
1298 audio = hb_list_item(job->list_audio, ii);
1299 st = audio->priv.mux_data->st;
1301 switch (audio->config.out.codec & HB_ACODEC_MASK)
1303 case HB_ACODEC_FFFLAC:
1304 case HB_ACODEC_FFFLAC24:
1305 if( audio->priv.config.extradata.length )
1307 uint8_t *priv_data;
1308 int priv_size;
1310 priv_size = audio->priv.config.extradata.length;
1311 priv_data = av_realloc(st->codec->extradata, priv_size);
1312 if (priv_data == NULL)
1314 break;
1316 memcpy(priv_data,
1317 audio->priv.config.extradata.bytes,
1318 audio->priv.config.extradata.length);
1319 st->codec->extradata = priv_data;
1320 st->codec->extradata_size = priv_size;
1322 break;
1323 default:
1324 break;
1328 av_write_trailer(m->oc);
1329 avio_close(m->oc->pb);
1330 avformat_free_context(m->oc);
1331 free(m->tracks);
1332 m->oc = NULL;
1334 return 0;
1337 hb_mux_object_t * hb_mux_avformat_init( hb_job_t * job )
1339 hb_mux_object_t * m = calloc( sizeof( hb_mux_object_t ), 1 );
1340 m->init = avformatInit;
1341 m->mux = avformatMux;
1342 m->end = avformatEnd;
1343 m->job = job;
1344 return m;