3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.m0k.org/>.
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;
34 /**********************************************************************
36 **********************************************************************
37 * Allocates hb_mux_data_t structures, create file and write headers
38 *********************************************************************/
39 static int MKVInit( hb_mux_object_t * m )
41 hb_job_t * job = m->job;
42 hb_title_t * title = job->title;
44 hb_mux_data_t * mux_data;
47 uint8_t default_track_flag = 1;
49 ogg_packet *ogg_headers[3];
50 mk_TrackConfig *track;
52 track = calloc(1, sizeof(mk_TrackConfig));
54 m->file = mk_createWriter(job->file, 1000000, 1);
57 mux_data = calloc(1, sizeof( hb_mux_data_t ) );
58 job->mux_data = mux_data;
60 track->trackType = MK_TRACK_VIDEO;
61 track->flagDefault = 1;
65 track->codecID = MK_VCODEC_MP4AVC;
66 /* Taken from x264 muxers.c */
67 avcC_len = 5 + 1 + 2 + job->config.h264.sps_length + 1 + 2 + job->config.h264.pps_length;
68 avcC = malloc(avcC_len);
73 avcC[1] = job->config.h264.sps[1]; /* AVCProfileIndication */
74 avcC[2] = job->config.h264.sps[2]; /* profile_compat */
75 avcC[3] = job->config.h264.sps[3]; /* AVCLevelIndication */
76 avcC[4] = 0xff; // nalu size length is four bytes
77 avcC[5] = 0xe1; // one sps
79 avcC[6] = job->config.h264.sps_length >> 8;
80 avcC[7] = job->config.h264.sps_length;
82 memcpy(avcC+8, job->config.h264.sps, job->config.h264.sps_length);
84 avcC[8+job->config.h264.sps_length] = 1; // one pps
85 avcC[9+job->config.h264.sps_length] = job->config.h264.pps_length >> 8;
86 avcC[10+job->config.h264.sps_length] = job->config.h264.pps_length;
88 memcpy( avcC+11+job->config.h264.sps_length, job->config.h264.pps, job->config.h264.pps_length );
89 track->codecPrivate = avcC;
90 track->codecPrivateSize = avcC_len;
95 case HB_VCODEC_FFMPEG:
96 track->codecID = MK_VCODEC_MP4ASP;
97 track->codecPrivate = job->config.mpeg4.bytes;
98 track->codecPrivateSize = job->config.mpeg4.length;
102 hb_log("muxmkv: Unknown video codec: %x", job->vcodec);
106 track->video.pixelWidth = job->width;
107 track->video.pixelHeight = job->height;
108 track->video.displayHeight = job->height;
111 track->video.displayWidth = job->width * ((double)job->pixel_aspect_width / (double)job->pixel_aspect_height);
115 track->video.displayWidth = job->width;
119 track->defaultDuration = (int64_t)(((float)job->vrate_base / (float)job->vrate) * 1000000000);
121 mux_data->track = mk_createTrack(m->file, track);
123 memset(track, 0, sizeof(mk_TrackConfig));
125 /* add the audio tracks */
126 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
128 audio = hb_list_item( title->list_audio, i );
129 mux_data = malloc( sizeof( hb_mux_data_t ) );
130 audio->mux_data = mux_data;
135 track->codecPrivate = NULL;
136 track->codecPrivateSize = 0;
137 track->codecID = MK_ACODEC_AC3;
140 track->codecPrivate = NULL;
141 track->codecPrivateSize = 0;
142 track->codecID = MK_ACODEC_MP3;
144 case HB_ACODEC_VORBIS:
150 track->codecID = MK_ACODEC_VORBIS;
151 cp_size = sizeof( char );
152 for (i = 0; i < 3; ++i)
154 ogg_headers[i] = (ogg_packet *)audio->config.vorbis.headers[i];
155 ogg_headers[i]->packet = (unsigned char *)&audio->config.vorbis.headers[i] + sizeof( ogg_packet );
156 cp_size += (sizeof( char ) * ((ogg_headers[i]->bytes / 255) + 1)) + ogg_headers[i]->bytes;
157 /* This will be too big, but it doesn't matter, as we only need it to be big enough. */
159 cp = track->codecPrivate = calloc(1, cp_size);
161 for (i = 0; i < 2; ++i)
163 for (j = ogg_headers[i]->bytes; j >= 255; j -= 255)
169 for(i = 0; i < 3; ++i)
171 memcpy(cp + offset, ogg_headers[i]->packet, ogg_headers[i]->bytes);
172 offset += ogg_headers[i]->bytes;
174 track->codecPrivateSize = offset;
178 track->codecPrivate = audio->config.aac.bytes;
179 track->codecPrivateSize = audio->config.aac.length;
180 track->codecID = MK_ACODEC_AAC;
184 hb_log("muxmkv: Unknown audio codec: %x", job->acodec);
188 if (default_track_flag)
190 track->flagDefault = 1;
191 default_track_flag = 0;
194 track->trackType = MK_TRACK_AUDIO;
195 track->language = audio->iso639_2;
196 track->audio.samplingFreq = (float)job->arate;
197 track->audio.channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->amixdown);
198 // track->defaultDuration = job->arate * 1000;
199 mux_data->track = mk_createTrack(m->file, track);
200 if (job->acodec == HB_ACODEC_VORBIS && track->codecPrivate != NULL)
201 free(track->codecPrivate);
204 mk_writeHeader( m->file, "HandBrake " HB_VERSION);
213 static int MKVMux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
216 hb_job_t * job = m->job;
217 hb_title_t * title = job->title;
218 uint64_t timecode = 0;
219 hb_chapter_t *chapter_data;
220 char tmp_buffer[1024];
221 char *string = tmp_buffer;
222 if (mux_data == job->mux_data)
225 if ((job->vcodec == HB_VCODEC_X264) && (buf->frametype & HB_FRAME_REF))
227 timecode = (buf->start + (buf->renderOffset - 1000000)) * TIMECODE_SCALE;
231 timecode = buf->start * TIMECODE_SCALE;
234 if (job->chapter_markers && (buf->new_chap || timecode == 0))
236 /* Make sure we're not writing a chapter that has 0 length */
237 if (mux_data->prev_chapter_tc != timecode)
239 chapter_data = hb_list_item( title->list_chapter, mux_data->current_chapter++ );
240 tmp_buffer[0] = '\0';
242 if( chapter_data != NULL )
244 string = chapter_data->title;
247 if( strlen(string) == 0 || strlen(string) >= 1024 )
249 snprintf( tmp_buffer, 1023, "Chapter %02i", mux_data->current_chapter );
252 mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, mux_data->prev_chapter_tc, string);
254 mux_data->prev_chapter_tc = timecode;
260 timecode = buf->start * TIMECODE_SCALE;
261 if (job->acodec == HB_ACODEC_VORBIS)
263 /* ughhh, vorbis is a pain :( */
266 op = (ogg_packet *)buf->data;
267 op->packet = buf->data + sizeof( ogg_packet );
268 mk_startFrame(m->file, mux_data->track);
269 mk_addFrameData(m->file, mux_data->track, op->packet, op->bytes);
270 mk_setFrameFlags(m->file, mux_data->track, timecode, 1);
275 mk_startFrame(m->file, mux_data->track);
276 mk_addFrameData(m->file, mux_data->track, buf->data, buf->size);
277 mk_setFrameFlags(m->file, mux_data->track, timecode,
278 ((job->vcodec == HB_VCODEC_X264 && mux_data == job->mux_data) ? (buf->frametype == HB_FRAME_IDR) : ((buf->frametype & HB_FRAME_KEY) != 0)) );
282 static int MKVEnd( hb_mux_object_t * m )
284 hb_job_t *job = m->job;
285 hb_mux_data_t *mux_data = job->mux_data;
286 hb_title_t *title = job->title;
287 hb_chapter_t *chapter_data = hb_list_item( title->list_chapter, mux_data->current_chapter++ );
288 char tmp_buffer[1024];
289 char *string = tmp_buffer;
291 if(job->chapter_markers)
293 tmp_buffer[0] = '\0';
295 if( chapter_data != NULL )
297 string = chapter_data->title;
300 if( strlen(string) == 0 || strlen(string) >= 1024 )
302 snprintf( tmp_buffer, 1023, "Chapter %02i", mux_data->current_chapter );
305 mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, mux_data->prev_chapter_tc, string);
310 // TODO: Free what we alloc'd
315 hb_mux_object_t * hb_mux_mkv_init( hb_job_t * job )
317 hb_mux_object_t * m = calloc( sizeof( hb_mux_object_t ), 1 );