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
11 #include "libavformat/avformat.h"
12 #include "libavutil/avstring.h"
13 #include "libavutil/intreadwrite.h"
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
49 hb_mux_data_t
** tracks
;
51 int64_t chapter_delay
;
76 const char *metadata_keys
[META_LAST
][META_MUX_LAST
] =
80 {"album_artist", "DIRECTOR"},
81 {"composer", "COMPOSER"},
82 {"date", "DATE_RELEASED"},
83 {"comment", "SUMMARY"},
86 {"description", "DESCRIPTION"},
87 {"synopsis", "SYNOPSIS"}
90 static char* lookup_lang_code(int mux
, char *iso639_2
)
101 // MKV lang codes should be ISO-639-2B if it exists,
103 lang
= lang_for_code2( iso639_2
);
104 out
= lang
->iso639_2b
? lang
->iso639_2b
: lang
->iso639_2
;
112 /**********************************************************************
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
;
121 hb_mux_data_t
* track
;
126 const char *muxer_name
= NULL
;
128 uint8_t default_track_flag
= 1;
129 uint8_t need_fonts
= 0;
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();
141 hb_error( "Could not initialize avformat context." );
145 AVDictionary
* av_opts
= NULL
;
149 m
->time_base
.num
= 1;
150 m
->time_base
.den
= 90000;
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);
161 av_dict_set(&av_opts
, "movflags", "+disable_chpl", 0);
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
;
175 hb_error("Invalid Mux %x", job
->mux
);
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
);
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
);
190 hb_error( "avio_open2 failed, errno %d", ret
);
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");
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
;
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");
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
);
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");
265 memcpy(priv_data
, job
->config
.mpeg4
.bytes
, priv_size
);
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");
281 memcpy(priv_data
, job
->config
.mpeg4
.bytes
, priv_size
);
285 case HB_VCODEC_FFMPEG_VP8
:
286 track
->st
->codec
->codec_id
= AV_CODEC_ID_VP8
;
291 case HB_VCODEC_THEORA
:
293 track
->st
->codec
->codec_id
= AV_CODEC_ID_THEORA
;
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;
305 priv_data
= av_malloc(priv_size
);
306 if (priv_data
== NULL
)
308 hb_error("Theora extradata: malloc failure");
313 for(ii
= 0; ii
< 3; ii
++)
315 AV_WB16(priv_data
+ size
, ogg_headers
[ii
]->bytes
);
317 memcpy(priv_data
+size
, ogg_headers
[ii
]->packet
,
318 ogg_headers
[ii
]->bytes
);
319 size
+= ogg_headers
[ii
]->bytes
;
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");
335 memcpy(priv_data
, job
->config
.h265
.headers
, priv_size
);
340 hb_error("muxavformat: Unknown video codec: %x", job
->vcodec
);
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
;
355 if( job
->pass_id
== HB_PASS_ENCODE_2ND
)
357 hb_interjob_t
* interjob
= hb_interjob_get( job
->h
);
358 vrate
= interjob
->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
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
;
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
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
;
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");
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
;
429 track
->st
->codec
->time_base
= m
->time_base
;
430 track
->st
->time_base
= m
->time_base
;
435 switch (audio
->config
.out
.codec
& HB_ACODEC_MASK
)
438 case HB_ACODEC_DCA_HD
:
439 track
->st
->codec
->codec_id
= AV_CODEC_ID_DTS
;
442 track
->st
->codec
->codec_id
= AV_CODEC_ID_AC3
;
444 case HB_ACODEC_FFEAC3
:
445 track
->st
->codec
->codec_id
= AV_CODEC_ID_EAC3
;
447 case HB_ACODEC_FFTRUEHD
:
448 track
->st
->codec
->codec_id
= AV_CODEC_ID_TRUEHD
;
452 track
->st
->codec
->codec_id
= AV_CODEC_ID_MP3
;
454 case HB_ACODEC_VORBIS
:
456 track
->st
->codec
->codec_id
= AV_CODEC_ID_VORBIS
;
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;
468 priv_data
= av_malloc(priv_size
);
469 if (priv_data
== NULL
)
471 hb_error("Vorbis extradata: malloc failure");
476 for(jj
= 0; jj
< 3; jj
++)
478 AV_WB16(priv_data
+ size
, ogg_headers
[jj
]->bytes
);
480 memcpy(priv_data
+size
, ogg_headers
[jj
]->packet
,
481 ogg_headers
[jj
]->bytes
);
482 size
+= ogg_headers
[jj
]->bytes
;
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");
499 audio
->priv
.config
.extradata
.bytes
,
500 audio
->priv
.config
.extradata
.length
);
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
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");
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
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");
535 hb_error("muxavformat: Unknown audio codec: %x",
536 audio
->config
.out
.codec
);
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
);
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
;
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
);
566 if (audio
->config
.out
.name
== NULL
)
568 switch (track
->st
->codec
->channels
)
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
593 av_dict_set(&track
->st
->metadata
, "handler", name
, 0);
600 "scale: 100%%, 100%%\n"
603 "fadein/out: 50, 50\n"
604 "align: OFF at LEFT TOP\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
;
640 subtitle
= hb_list_item( job
->list_subtitle
, ii
);
641 if (subtitle
->config
.dest
!= PASSTHRUSUB
)
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");
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
;
665 switch (subtitle
->source
)
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
,
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]);
683 priv_data
= av_malloc(priv_size
);
684 if (priv_data
== NULL
)
686 hb_error("VOBSUB extradata: malloc failure");
689 memcpy(priv_data
, subidx
, priv_size
);
694 track
->st
->codec
->codec_id
= AV_CODEC_ID_HDMV_PGS_SUBTITLE
;
704 if (job
->mux
== HB_MUX_AV_MP4
)
706 track
->st
->codec
->codec_id
= AV_CODEC_ID_MOV_TEXT
;
710 track
->st
->codec
->codec_id
= AV_CODEC_ID_SSA
;
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");
722 memcpy(priv_data
, subtitle
->extradata
, priv_size
);
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
746 0xff, 0xff, 0xff, 0xff, // Fg color
748 0x00, 0x00, 0x00, 0x12, // Font table size
749 'f','t','a','b', // Tag
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");
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
);
789 av_dict_set(&track
->st
->metadata
, "language", lang
, 0);
795 hb_list_t
* list_attachment
= job
->list_attachment
;
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
);
807 hb_error("Could not initialize attachment stream");
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");
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);
842 hb_metadata_t
*md
= job
->metadata
;
844 hb_deep_log(2, "Writing Metadata to output file...");
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);
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);
865 metadata_keys
[META_COMPOSER
][meta_mux
] != NULL
)
867 av_dict_set(&m
->oc
->metadata
,
868 metadata_keys
[META_COMPOSER
][meta_mux
],
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);
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);
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
],
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
);
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
);
925 av_dict_free( &av_opts
);
926 hb_error( "muxavformat: avformat_write_header failed!");
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
);
941 job
->mux_data
= NULL
;
942 avformat_free_context(m
->oc
);
943 *job
->done_error
= HB_ERROR_INIT
;
948 static int add_chapter(hb_mux_object_t
*m
, int64_t start
, int64_t end
, char * title
)
951 AVChapter
**chapters
;
952 int nchap
= m
->oc
->nb_chapters
;
955 chapters
= av_realloc(m
->oc
->chapters
, nchap
* sizeof(AVChapter
*));
956 if (chapters
== NULL
)
958 hb_error("chapter array: malloc failure");
962 chap
= av_mallocz(sizeof(AVChapter
));
965 hb_error("chapter: malloc failure");
969 m
->oc
->chapters
= chapters
;
970 m
->oc
->chapters
[nchap
-1] = chap
;
971 m
->oc
->nb_chapters
= 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
;
981 av_dict_set(&chap
->metadata
, "title", title
, 0);
986 static int avformatMux(hb_mux_object_t
*m
, hb_mux_data_t
*track
, hb_buffer_t
*buf
)
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
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
))
1012 // delay by one frame so that we can compute duration properly.
1013 tmp
= track
->delay_buf
;
1014 track
->delay_buf
= buf
;
1020 if (buf
->s
.renderOffset
== AV_NOPTS_VALUE
)
1022 dts
= av_rescale_q(buf
->s
.start
, (AVRational
){1,90000},
1023 track
->st
->time_base
);
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
);
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
);
1061 av_init_packet(&pkt
);
1062 pkt
.data
= buf
->data
;
1063 pkt
.size
= buf
->size
;
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
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
)
1101 if (chapter
->title
!= NULL
)
1103 snprintf(title
, 1023, "%s", chapter
->title
);
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
;
1116 case MUX_TYPE_SUBTITLE
:
1118 if (job
->mux
== HB_MUX_AV_MP4
)
1120 /* Write an empty sample */
1121 if ( track
->duration
< pts
)
1124 uint8_t empty
[2] = {0,0};
1126 av_init_packet(&empty_pkt
);
1127 empty_pkt
.data
= empty
;
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
);
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
;
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;
1156 * Copy the subtitle into buffer stripping markup and creating
1157 * style atoms for them.
1159 hb_muxmp4_process_subtitle_style( buf
->data
,
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;
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
;
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
, ',');
1196 // Skip the layer field
1197 layer
= strtol(ssa
, NULL
, 10);
1198 ssa
= strchr(ssa
, ',');
1201 sprintf((char*)sub_out
,
1202 "Dialogue: %d,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s",
1204 start_hh
, start_mm
, start_ss
, start_ms
,
1205 stop_hh
, stop_mm
, stop_ss
, stop_ms
, ssa
);
1207 pkt
.size
= strlen((char*)sub_out
) + 1;
1209 pkt
.convergence_duration
= pkt
.duration
;
1212 case MUX_TYPE_AUDIO
:
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)
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
;
1239 hb_buffer_close( &buf
);
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.
1256 // Flush any delayed frames
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)
1279 if (chapter
->title
!= NULL
)
1281 snprintf(title
, 1023, "%s", chapter
->title
);
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
1293 for(ii
= 0; ii
< hb_list_count( job
->list_audio
); ii
++)
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
)
1310 priv_size
= audio
->priv
.config
.extradata
.length
;
1311 priv_data
= av_realloc(st
->codec
->extradata
, priv_size
);
1312 if (priv_data
== NULL
)
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
;
1328 av_write_trailer(m
->oc
);
1329 avio_close(m
->oc
->pb
);
1330 avformat_free_context(m
->oc
);
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
;