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;
62 track->flagEnabled = 1;
66 track->codecID = MK_VCODEC_MP4AVC;
67 /* Taken from x264 muxers.c */
68 avcC_len = 5 + 1 + 2 + job->config.h264.sps_length + 1 + 2 + job->config.h264.pps_length;
69 avcC = malloc(avcC_len);
74 avcC[1] = job->config.h264.sps[1]; /* AVCProfileIndication */
75 avcC[2] = job->config.h264.sps[2]; /* profile_compat */
76 avcC[3] = job->config.h264.sps[3]; /* AVCLevelIndication */
77 avcC[4] = 0xff; // nalu size length is four bytes
78 avcC[5] = 0xe1; // one sps
80 avcC[6] = job->config.h264.sps_length >> 8;
81 avcC[7] = job->config.h264.sps_length;
83 memcpy(avcC+8, job->config.h264.sps, job->config.h264.sps_length);
85 avcC[8+job->config.h264.sps_length] = 1; // one pps
86 avcC[9+job->config.h264.sps_length] = job->config.h264.pps_length >> 8;
87 avcC[10+job->config.h264.sps_length] = job->config.h264.pps_length;
89 memcpy( avcC+11+job->config.h264.sps_length, job->config.h264.pps, job->config.h264.pps_length );
90 track->codecPrivate = avcC;
91 track->codecPrivateSize = avcC_len;
96 case HB_VCODEC_FFMPEG:
97 track->codecID = MK_VCODEC_MP4ASP;
98 track->codecPrivate = job->config.mpeg4.bytes;
99 track->codecPrivateSize = job->config.mpeg4.length;
103 hb_error("muxmkv: Unknown video codec: %x", job->vcodec);
107 track->video.pixelWidth = job->width;
108 track->video.pixelHeight = job->height;
109 track->video.displayHeight = job->height;
112 track->video.displayWidth = job->width * ((double)job->pixel_aspect_width / (double)job->pixel_aspect_height);
116 track->video.displayWidth = job->width;
120 track->defaultDuration = (int64_t)(((float)job->vrate_base / (float)job->vrate) * 1000000000);
122 mux_data->track = mk_createTrack(m->file, track);
124 memset(track, 0, sizeof(mk_TrackConfig));
126 /* add the audio tracks */
127 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
129 audio = hb_list_item( title->list_audio, i );
130 mux_data = malloc( sizeof( hb_mux_data_t ) );
131 audio->mux_data = mux_data;
136 track->codecPrivate = NULL;
137 track->codecPrivateSize = 0;
138 track->codecID = MK_ACODEC_AC3;
141 track->codecPrivate = NULL;
142 track->codecPrivateSize = 0;
143 track->codecID = MK_ACODEC_MP3;
145 case HB_ACODEC_VORBIS:
148 uint64_t cp_size = 0;
149 track->codecID = MK_ACODEC_VORBIS;
150 uint64_t header_sizes[3];
151 for (i = 0; i < 3; ++i)
153 ogg_headers[i] = (ogg_packet *)audio->config.vorbis.headers[i];
154 ogg_headers[i]->packet = (unsigned char *)&audio->config.vorbis.headers[i] + sizeof( ogg_packet );
155 header_sizes[i] = ogg_headers[i]->bytes;
157 track->codecPrivate = mk_laceXiph(header_sizes, 2, &cp_size);
158 track->codecPrivate = realloc(track->codecPrivate, cp_size + ogg_headers[0]->bytes + ogg_headers[1]->bytes + ogg_headers[2]->bytes);
159 for(i = 0; i < 3; ++i)
161 memcpy(track->codecPrivate + cp_size, ogg_headers[i]->packet, ogg_headers[i]->bytes);
162 cp_size += ogg_headers[i]->bytes;
164 track->codecPrivateSize = cp_size;
168 track->codecPrivate = audio->config.aac.bytes;
169 track->codecPrivateSize = audio->config.aac.length;
170 track->codecID = MK_ACODEC_AAC;
174 hb_error("muxmkv: Unknown audio codec: %x", job->acodec);
178 if (default_track_flag)
180 track->flagDefault = 1;
181 default_track_flag = 0;
183 track->flagEnabled = 1;
184 track->trackType = MK_TRACK_AUDIO;
185 track->language = audio->iso639_2;
186 track->audio.samplingFreq = (float)job->arate;
187 track->audio.channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->amixdown);
188 // track->defaultDuration = job->arate * 1000;
189 mux_data->track = mk_createTrack(m->file, track);
190 if (job->acodec == HB_ACODEC_VORBIS && track->codecPrivate != NULL)
191 free(track->codecPrivate);
194 if( mk_writeHeader( m->file, "HandBrake " HB_VERSION) < 0 )
196 hb_error( "Failed to write to output file, disk full?");
207 static int MKVMux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
210 hb_job_t * job = m->job;
211 hb_title_t * title = job->title;
212 uint64_t timecode = 0;
213 hb_chapter_t *chapter_data;
214 char tmp_buffer[1024];
215 char *string = tmp_buffer;
216 if (mux_data == job->mux_data)
219 if ((job->vcodec == HB_VCODEC_X264) && (buf->frametype & HB_FRAME_REF))
221 timecode = (buf->start + (buf->renderOffset - 1000000)) * TIMECODE_SCALE;
225 timecode = buf->start * TIMECODE_SCALE;
228 if (job->chapter_markers && (buf->new_chap || timecode == 0))
230 /* Make sure we're not writing a chapter that has 0 length */
231 if (mux_data->prev_chapter_tc != timecode)
233 chapter_data = hb_list_item( title->list_chapter, mux_data->current_chapter++ );
234 tmp_buffer[0] = '\0';
236 if( chapter_data != NULL )
238 string = chapter_data->title;
241 if( strlen(string) == 0 || strlen(string) >= 1024 )
243 snprintf( tmp_buffer, 1023, "Chapter %02i", mux_data->current_chapter );
246 mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, mux_data->prev_chapter_tc, string);
248 mux_data->prev_chapter_tc = timecode;
254 timecode = buf->start * TIMECODE_SCALE;
255 if (job->acodec == HB_ACODEC_VORBIS)
257 /* ughhh, vorbis is a pain :( */
260 op = (ogg_packet *)buf->data;
261 op->packet = buf->data + sizeof( ogg_packet );
262 mk_startFrame(m->file, mux_data->track);
263 mk_addFrameData(m->file, mux_data->track, op->packet, op->bytes);
264 mk_setFrameFlags(m->file, mux_data->track, timecode, 1);
269 if( mk_startFrame(m->file, mux_data->track) < 0)
271 hb_error( "Failed to write start frame to output file, Disk Full?" );
274 mk_addFrameData(m->file, mux_data->track, buf->data, buf->size);
275 mk_setFrameFlags(m->file, mux_data->track, timecode,
276 ((job->vcodec == HB_VCODEC_X264 && mux_data == job->mux_data) ? (buf->frametype == HB_FRAME_IDR) : ((buf->frametype & HB_FRAME_KEY) != 0)) );
280 static int MKVEnd( hb_mux_object_t * m )
282 hb_job_t *job = m->job;
283 hb_mux_data_t *mux_data = job->mux_data;
284 hb_title_t *title = job->title;
285 hb_chapter_t *chapter_data = hb_list_item( title->list_chapter, mux_data->current_chapter++ );
286 char tmp_buffer[1024];
287 char *string = tmp_buffer;
289 if(job->chapter_markers)
291 tmp_buffer[0] = '\0';
293 if( chapter_data != NULL )
295 string = chapter_data->title;
298 if( strlen(string) == 0 || strlen(string) >= 1024 )
300 snprintf( tmp_buffer, 1023, "Chapter %02i", mux_data->current_chapter );
303 mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, mux_data->prev_chapter_tc, string);
306 if( mk_close(m->file) < 0 )
308 hb_error( "Failed to flush the last frame and close the output file, Disk Full?" );
312 // TODO: Free what we alloc'd
317 hb_mux_object_t * hb_mux_mkv_init( hb_job_t * job )
319 hb_mux_object_t * m = calloc( sizeof( hb_mux_object_t ), 1 );