3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
14 /* Scale factor to apply to timecodes to convert from HandBrake's
15 * 1/90000s to nanoseconds as expected by libmkv */
16 #define TIMECODE_SCALE 1000000000 / 90000
18 struct hb_mux_object_s
30 uint64_t prev_chapter_tc;
31 uint16_t current_chapter;
37 /**********************************************************************
39 **********************************************************************
40 * Allocates hb_mux_data_t structures, create file and write headers
41 *********************************************************************/
42 static int MKVInit( hb_mux_object_t * m )
44 hb_job_t * job = m->job;
45 hb_title_t * title = job->title;
47 hb_mux_data_t * mux_data;
50 uint8_t default_track_flag = 1;
52 ogg_packet *ogg_headers[3];
53 mk_TrackConfig *track;
55 track = calloc(1, sizeof(mk_TrackConfig));
57 m->file = mk_createWriter(job->file, 1000000, 1);
61 hb_error( "Could not create output file, Disk Full?" );
68 mux_data = calloc(1, sizeof( hb_mux_data_t ) );
69 job->mux_data = mux_data;
71 track->trackType = MK_TRACK_VIDEO;
72 track->flagDefault = 1;
73 track->flagEnabled = 1;
77 track->codecID = MK_VCODEC_MP4AVC;
78 /* Taken from x264 muxers.c */
79 avcC_len = 5 + 1 + 2 + job->config.h264.sps_length + 1 + 2 + job->config.h264.pps_length;
80 avcC = malloc(avcC_len);
85 avcC[1] = job->config.h264.sps[1]; /* AVCProfileIndication */
86 avcC[2] = job->config.h264.sps[2]; /* profile_compat */
87 avcC[3] = job->config.h264.sps[3]; /* AVCLevelIndication */
88 avcC[4] = 0xff; // nalu size length is four bytes
89 avcC[5] = 0xe1; // one sps
91 avcC[6] = job->config.h264.sps_length >> 8;
92 avcC[7] = job->config.h264.sps_length;
94 memcpy(avcC+8, job->config.h264.sps, job->config.h264.sps_length);
96 avcC[8+job->config.h264.sps_length] = 1; // one pps
97 avcC[9+job->config.h264.sps_length] = job->config.h264.pps_length >> 8;
98 avcC[10+job->config.h264.sps_length] = job->config.h264.pps_length;
100 memcpy( avcC+11+job->config.h264.sps_length, job->config.h264.pps, job->config.h264.pps_length );
101 track->codecPrivate = avcC;
102 track->codecPrivateSize = avcC_len;
106 case HB_VCODEC_FFMPEG:
107 track->codecID = MK_VCODEC_MP4ASP;
108 track->codecPrivate = job->config.mpeg4.bytes;
109 track->codecPrivateSize = job->config.mpeg4.length;
111 case HB_VCODEC_THEORA:
114 uint64_t cp_size = 0;
115 track->codecID = MK_VCODEC_THEORA;
116 uint64_t header_sizes[3];
117 for (i = 0; i < 3; ++i)
119 ogg_headers[i] = (ogg_packet *)job->config.theora.headers[i];
120 ogg_headers[i]->packet = (unsigned char *)&job->config.theora.headers[i] + sizeof( ogg_packet );
121 header_sizes[i] = ogg_headers[i]->bytes;
123 track->codecPrivate = mk_laceXiph(header_sizes, 2, &cp_size);
124 track->codecPrivate = realloc(track->codecPrivate, cp_size + ogg_headers[0]->bytes + ogg_headers[1]->bytes + ogg_headers[2]->bytes);
125 for(i = 0; i < 3; ++i)
127 memcpy(track->codecPrivate + cp_size, ogg_headers[i]->packet, ogg_headers[i]->bytes);
128 cp_size += ogg_headers[i]->bytes;
130 track->codecPrivateSize = cp_size;
135 hb_error("muxmkv: Unknown video codec: %x", job->vcodec);
139 track->extra.video.pixelWidth = job->width;
140 track->extra.video.pixelHeight = job->height;
141 track->extra.video.displayHeight = job->height;
142 if( job->anamorphic.mode )
144 track->extra.video.displayWidth = job->width * ((double)job->anamorphic.par_width / (double)job->anamorphic.par_height);
148 track->extra.video.displayWidth = job->width;
152 track->defaultDuration = (int64_t)(((float)job->vrate_base / (float)job->vrate) * 1000000000);
154 mux_data->track = mk_createTrack(m->file, track);
156 memset(track, 0, sizeof(mk_TrackConfig));
158 /* add the audio tracks */
159 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
161 audio = hb_list_item( title->list_audio, i );
162 mux_data = calloc(1, sizeof( hb_mux_data_t ) );
163 audio->priv.mux_data = mux_data;
165 mux_data->codec = audio->config.out.codec;
167 switch (audio->config.out.codec)
170 case HB_ACODEC_DCA_PASS:
171 track->codecPrivate = NULL;
172 track->codecPrivateSize = 0;
173 track->codecID = MK_ACODEC_DTS;
176 case HB_ACODEC_AC3_PASS:
177 track->codecPrivate = NULL;
178 track->codecPrivateSize = 0;
179 track->codecID = MK_ACODEC_AC3;
182 track->codecPrivate = NULL;
183 track->codecPrivateSize = 0;
184 track->codecID = MK_ACODEC_MP3;
186 case HB_ACODEC_VORBIS:
189 uint64_t cp_size = 0;
190 track->codecID = MK_ACODEC_VORBIS;
191 uint64_t header_sizes[3];
192 for (i = 0; i < 3; ++i)
194 ogg_headers[i] = (ogg_packet *)audio->priv.config.vorbis.headers[i];
195 ogg_headers[i]->packet = (unsigned char *)&audio->priv.config.vorbis.headers[i] + sizeof( ogg_packet );
196 header_sizes[i] = ogg_headers[i]->bytes;
198 track->codecPrivate = mk_laceXiph(header_sizes, 2, &cp_size);
199 track->codecPrivate = realloc(track->codecPrivate, cp_size + ogg_headers[0]->bytes + ogg_headers[1]->bytes + ogg_headers[2]->bytes);
200 for(i = 0; i < 3; ++i)
202 memcpy(track->codecPrivate + cp_size, ogg_headers[i]->packet, ogg_headers[i]->bytes);
203 cp_size += ogg_headers[i]->bytes;
205 track->codecPrivateSize = cp_size;
209 case HB_ACODEC_CA_AAC:
210 track->codecPrivate = audio->priv.config.aac.bytes;
211 track->codecPrivateSize = audio->priv.config.aac.length;
212 track->codecID = MK_ACODEC_AAC;
216 hb_error("muxmkv: Unknown audio codec: %x", audio->config.out.codec);
220 if (default_track_flag)
222 track->flagDefault = 1;
223 default_track_flag = 0;
227 track->flagDefault = 0;
229 track->flagEnabled = 1;
230 track->trackType = MK_TRACK_AUDIO;
231 track->language = audio->config.lang.iso639_2;
232 track->extra.audio.samplingFreq = (float)audio->config.out.samplerate;
233 if (audio->config.out.codec == HB_ACODEC_AC3_PASS ||
234 audio->config.out.codec == HB_ACODEC_DCA_PASS)
236 track->extra.audio.channels = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT(audio->config.in.channel_layout);
240 track->extra.audio.channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
242 // track->defaultDuration = job->arate * 1000;
243 mux_data->track = mk_createTrack(m->file, track);
244 if (audio->config.out.codec == HB_ACODEC_VORBIS && track->codecPrivate != NULL)
245 free(track->codecPrivate);
251 "scale: 100%%, 100%%\n"
254 "fadein/out: 50, 50\n"
255 "align: OFF at LEFT TOP\n"
258 "palette: %06x, %06x, %06x, %06x, %06x, %06x, "
259 "%06x, %06x, %06x, %06x, %06x, %06x, %06x, %06x, %06x, %06x\n"
260 "custom colors: OFF, tridx: 0000, "
261 "colors: 000000, 000000, 000000, 000000\n";
263 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
265 hb_subtitle_t * subtitle;
270 subtitle = hb_list_item( title->list_subtitle, i );
271 if (subtitle->config.dest != PASSTHRUSUB)
274 memset(track, 0, sizeof(mk_TrackConfig));
275 switch (subtitle->format)
278 track->codecID = MK_SUBTITLE_VOBSUB;
279 for (j = 0; j < 16; j++)
280 rgb[j] = hb_yuv2rgb(subtitle->palette[j]);
281 len = snprintf(subidx, 2048, subidx_fmt,
282 subtitle->width, subtitle->height,
284 rgb[0], rgb[1], rgb[2], rgb[3],
285 rgb[4], rgb[5], rgb[6], rgb[7],
286 rgb[8], rgb[9], rgb[10], rgb[11],
287 rgb[12], rgb[13], rgb[14], rgb[15]);
288 track->codecPrivate = subidx;
289 track->codecPrivateSize = len + 1;
292 track->codecID = MK_SUBTITLE_UTF8;
297 if ( subtitle->config.default_track )
299 track->flagDefault = 1;
302 mux_data = calloc(1, sizeof( hb_mux_data_t ) );
303 subtitle->mux_data = mux_data;
304 mux_data->subtitle = 1;
305 mux_data->sub_format = subtitle->format;
307 track->flagEnabled = 1;
308 track->trackType = MK_TRACK_SUBTITLE;
309 track->language = subtitle->iso639_2;
311 mux_data->track = mk_createTrack(m->file, track);
314 if( mk_writeHeader( m->file, "HandBrake " HB_PROJECT_VERSION) < 0 )
316 hb_error( "Failed to write to output file, disk full?");
327 static int MKVMux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
330 ogg_packet *op = NULL;
331 hb_job_t * job = m->job;
332 hb_title_t * title = job->title;
333 uint64_t timecode = 0;
334 hb_chapter_t *chapter_data;
335 char tmp_buffer[1024];
336 char *string = tmp_buffer;
338 if (mux_data == job->mux_data)
341 timecode = buf->start * TIMECODE_SCALE;
343 if (job->chapter_markers && (buf->new_chap || timecode == 0))
345 /* Make sure we're not writing a chapter that has 0 length */
346 if (mux_data->prev_chapter_tc != timecode)
350 mux_data->current_chapter = buf->new_chap - 2;
352 chapter_data = hb_list_item( title->list_chapter,
353 mux_data->current_chapter++ );
354 tmp_buffer[0] = '\0';
356 if( chapter_data != NULL )
358 string = chapter_data->title;
361 if( strlen(string) == 0 || strlen(string) >= 1024 )
363 snprintf( tmp_buffer, 1023, "Chapter %02i", mux_data->current_chapter );
366 mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, mux_data->prev_chapter_tc, string);
368 mux_data->prev_chapter_tc = timecode;
371 if (job->vcodec == HB_VCODEC_THEORA)
373 /* ughhh, theora is a pain :( */
374 op = (ogg_packet *)buf->data;
375 op->packet = buf->data + sizeof( ogg_packet );
376 if (mk_startFrame(m->file, mux_data->track) < 0)
378 hb_error( "Failed to write frame to output file, Disk Full?" );
381 mk_addFrameData(m->file, mux_data->track, op->packet, op->bytes);
382 mk_setFrameFlags(m->file, mux_data->track, timecode, 1, 0);
383 hb_buffer_close( &buf );
387 else if ( mux_data->subtitle )
390 timecode = buf->start * TIMECODE_SCALE;
391 if( mk_startFrame(m->file, mux_data->track) < 0)
393 hb_error( "Failed to write frame to output file, Disk Full?" );
397 duration = buf->stop * TIMECODE_SCALE - timecode;
398 if( mux_data->sub_format == TEXTSUB )
400 mk_addFrameData(m->file, mux_data->track, buf->data, buf->size);
401 mk_setFrameFlags(m->file, mux_data->track, timecode, 1, duration);
405 mk_addFrameData(m->file, mux_data->track, buf->data, buf->size);
406 mk_setFrameFlags(m->file, mux_data->track, timecode, 1, duration);
408 mk_flushFrame(m->file, mux_data->track);
409 hb_buffer_close( &buf );
415 timecode = buf->start * TIMECODE_SCALE;
416 if (mux_data->codec == HB_ACODEC_VORBIS)
418 /* ughhh, vorbis is a pain :( */
419 op = (ogg_packet *)buf->data;
420 op->packet = buf->data + sizeof( ogg_packet );
421 if (mk_startFrame(m->file, mux_data->track))
423 hb_error( "Failed to write frame to output file, Disk Full?" );
426 mk_addFrameData(m->file, mux_data->track, op->packet, op->bytes);
427 mk_setFrameFlags(m->file, mux_data->track, timecode, 1, 0);
428 hb_buffer_close( &buf );
433 if( mk_startFrame(m->file, mux_data->track) < 0)
435 hb_error( "Failed to write frame to output file, Disk Full?" );
438 mk_addFrameData(m->file, mux_data->track, buf->data, buf->size);
439 mk_setFrameFlags(m->file, mux_data->track, timecode,
440 ((job->vcodec == HB_VCODEC_X264 &&
441 mux_data == job->mux_data) ?
442 (buf->frametype == HB_FRAME_IDR) :
443 ((buf->frametype & HB_FRAME_KEY) != 0)), 0 );
444 hb_buffer_close( &buf );
448 static int MKVEnd( hb_mux_object_t * m )
450 hb_job_t *job = m->job;
451 hb_mux_data_t *mux_data = job->mux_data;
452 hb_title_t *title = job->title;
453 hb_chapter_t *chapter_data;
454 char tmp_buffer[1024];
455 char *string = tmp_buffer;
460 * We must have failed to create the file in the first place.
465 chapter_data = hb_list_item( title->list_chapter, mux_data->current_chapter++ );
467 if(job->chapter_markers)
469 tmp_buffer[0] = '\0';
471 if( chapter_data != NULL )
473 string = chapter_data->title;
476 if( strlen(string) == 0 || strlen(string) >= 1024 )
478 snprintf( tmp_buffer, 1023, "Chapter %02i", mux_data->current_chapter );
481 mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, mux_data->prev_chapter_tc, string);
484 if( title->metadata )
486 hb_metadata_t *md = title->metadata;
488 hb_deep_log( 2, "Writing Metadata to output file...");
489 mk_createTagSimple( m->file, MK_TAG_TITLE, md->name );
490 mk_createTagSimple( m->file, "ARTIST", md->artist );
491 mk_createTagSimple( m->file, "COMPOSER", md->composer );
492 mk_createTagSimple( m->file, MK_TAG_SYNOPSIS, md->comment );
493 mk_createTagSimple( m->file, "DATE_RELEASED", md->release_date );
494 // mk_createTagSimple( m->file, "", md->album );
495 mk_createTagSimple( m->file, MK_TAG_GENRE, md->genre );
498 if( mk_close(m->file) < 0 )
500 hb_error( "Failed to flush the last frame and close the output file, Disk Full?" );
504 // TODO: Free what we alloc'd
509 hb_mux_object_t * hb_mux_mkv_init( hb_job_t * job )
511 hb_mux_object_t * m = calloc( sizeof( hb_mux_object_t ), 1 );