OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / libhb / muxmkv.c
1 /* $Id:  $
2
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. */
6
7 /* libmkv header */
8 #include "libmkv.h"
9
10 #include <ogg/ogg.h>
11
12 #include "hb.h"
13
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
17
18 struct hb_mux_object_s
19 {
20     HB_MUX_COMMON;
21
22     hb_job_t * job;
23
24     mk_Writer * file;
25 };
26
27 struct hb_mux_data_s
28 {
29     mk_Track  * track;
30     uint64_t  prev_chapter_tc;
31     uint16_t  current_chapter;
32     int       codec;
33     int       subtitle;
34     int       sub_format;
35 };
36
37 /**********************************************************************
38  * MKVInit
39  **********************************************************************
40  * Allocates hb_mux_data_t structures, create file and write headers
41  *********************************************************************/
42 static int MKVInit( hb_mux_object_t * m )
43 {
44     hb_job_t   * job   = m->job;
45     hb_title_t * title = job->title;
46     hb_audio_t    * audio;
47     hb_mux_data_t * mux_data;
48
49     uint8_t         *avcC = NULL;
50     uint8_t         default_track_flag = 1;
51     int             avcC_len, i, j;
52     ogg_packet      *ogg_headers[3];
53     mk_TrackConfig *track;
54
55     track = calloc(1, sizeof(mk_TrackConfig));
56
57     m->file = mk_createWriter(job->file, 1000000, 1);
58
59     if( !m->file )
60     {
61         hb_error( "Could not create output file, Disk Full?" );
62         job->mux_data = NULL;
63         *job->die = 1;
64         return 0;
65     }
66
67     /* Video track */
68     mux_data      = calloc(1, sizeof( hb_mux_data_t ) );
69     job->mux_data = mux_data;
70
71     track->trackType = MK_TRACK_VIDEO;
72     track->flagDefault = 1;
73     track->flagEnabled = 1;
74     switch (job->vcodec)
75     {
76         case HB_VCODEC_X264:
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);
81             if (avcC == NULL)
82                 return -1;
83
84             avcC[0] = 1;
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
90
91             avcC[6] = job->config.h264.sps_length >> 8;
92             avcC[7] = job->config.h264.sps_length;
93
94             memcpy(avcC+8, job->config.h264.sps, job->config.h264.sps_length);
95
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;
99
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;
103             if (job->areBframes)
104                 track->minCache = 1;
105             break;
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;
110             break;
111         case HB_VCODEC_THEORA:
112             {
113                 int i;
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)
118                 {
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;
122                 }
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)
126                 {
127                     memcpy(track->codecPrivate + cp_size, ogg_headers[i]->packet, ogg_headers[i]->bytes);
128                     cp_size += ogg_headers[i]->bytes;
129                 }
130                 track->codecPrivateSize = cp_size;
131             }
132             break;
133         default:
134             *job->die = 1;
135             hb_error("muxmkv: Unknown video codec: %x", job->vcodec);
136             return 0;
137     }
138
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 )
143     {
144         track->extra.video.displayWidth = job->width * ((double)job->anamorphic.par_width / (double)job->anamorphic.par_height);
145     }
146     else
147     {
148         track->extra.video.displayWidth = job->width;
149     }
150
151
152     track->defaultDuration = (int64_t)(((float)job->vrate_base / (float)job->vrate) * 1000000000);
153
154     mux_data->track = mk_createTrack(m->file, track);
155
156     memset(track, 0, sizeof(mk_TrackConfig));
157
158     /* add the audio tracks */
159     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
160     {
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;
164
165         mux_data->codec = audio->config.out.codec;
166
167         switch (audio->config.out.codec)
168         {
169             case HB_ACODEC_DCA:
170             case HB_ACODEC_DCA_PASS:
171                 track->codecPrivate = NULL;
172                 track->codecPrivateSize = 0;
173                 track->codecID = MK_ACODEC_DTS;
174                 break;
175             case HB_ACODEC_AC3:
176             case HB_ACODEC_AC3_PASS:
177                 track->codecPrivate = NULL;
178                 track->codecPrivateSize = 0;
179                 track->codecID = MK_ACODEC_AC3;
180                 break;
181             case HB_ACODEC_LAME:
182                 track->codecPrivate = NULL;
183                 track->codecPrivateSize = 0;
184                 track->codecID = MK_ACODEC_MP3;
185                 break;
186             case HB_ACODEC_VORBIS:
187                 {
188                     int i;
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)
193                     {
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;
197                     }
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)
201                     {
202                         memcpy(track->codecPrivate + cp_size, ogg_headers[i]->packet, ogg_headers[i]->bytes);
203                         cp_size += ogg_headers[i]->bytes;
204                     }
205                     track->codecPrivateSize = cp_size;
206                 }
207                 break;
208             case HB_ACODEC_FAAC:
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;
213                 break;
214             default:
215                 *job->die = 1;
216                 hb_error("muxmkv: Unknown audio codec: %x", audio->config.out.codec);
217                 return 0;
218         }
219
220         if (default_track_flag)
221         {
222             track->flagDefault = 1;
223             default_track_flag = 0;
224         }
225         else
226         {
227             track->flagDefault = 0;
228         }
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)
235         {
236             track->extra.audio.channels = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT(audio->config.in.channel_layout);
237         }
238         else
239         {
240             track->extra.audio.channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
241         }
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);
246     }
247
248     char * subidx_fmt =
249         "size: %dx%d\n"
250         "org: %d, %d\n"
251         "scale: 100%%, 100%%\n"
252         "alpha: 100%%\n"
253         "smooth: OFF\n"
254         "fadein/out: 50, 50\n"
255         "align: OFF at LEFT TOP\n"
256         "time offset: 0\n"
257         "forced subs: %s\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";
262
263     for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
264     {
265         hb_subtitle_t * subtitle;
266         uint32_t        rgb[16];
267         char            subidx[2048];
268         int             len;
269
270         subtitle = hb_list_item( title->list_subtitle, i );
271         if (subtitle->config.dest != PASSTHRUSUB)
272             continue;
273
274         memset(track, 0, sizeof(mk_TrackConfig));
275         switch (subtitle->format)
276         {
277             case PICTURESUB:
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,
283                         0, 0, "OFF",
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;
290                 break;
291             case TEXTSUB:
292                 track->codecID = MK_SUBTITLE_UTF8;
293                 break;
294             default:
295                 continue;
296         }
297         if ( subtitle->config.default_track )
298         {
299             track->flagDefault = 1;
300         }
301
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;
306         
307         track->flagEnabled = 1;
308         track->trackType = MK_TRACK_SUBTITLE;
309         track->language = subtitle->iso639_2;
310
311         mux_data->track = mk_createTrack(m->file, track);
312     }
313
314     if( mk_writeHeader( m->file, "HandBrake " HB_PROJECT_VERSION) < 0 )
315     {
316         hb_error( "Failed to write to output file, disk full?");
317         *job->die = 1;
318     }
319     if (track != NULL)
320         free(track);
321     if (avcC != NULL)
322         free(avcC);
323
324     return 0;
325 }
326
327 static int MKVMux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
328                    hb_buffer_t * buf )
329 {
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;
337
338     if (mux_data == job->mux_data)
339     {
340         /* Video */
341         timecode = buf->start * TIMECODE_SCALE;
342
343         if (job->chapter_markers && (buf->new_chap || timecode == 0))
344         {
345             /* Make sure we're not writing a chapter that has 0 length */
346             if (mux_data->prev_chapter_tc != timecode)
347             {
348                 if ( buf->new_chap )
349                 {
350                     mux_data->current_chapter = buf->new_chap - 2;
351                 }
352                 chapter_data = hb_list_item( title->list_chapter,
353                                              mux_data->current_chapter++ );
354                 tmp_buffer[0] = '\0';
355
356                 if( chapter_data != NULL )
357                 {
358                     string = chapter_data->title;
359                 }
360
361                 if( strlen(string) == 0 || strlen(string) >= 1024 )
362                 {
363                     snprintf( tmp_buffer, 1023, "Chapter %02i", mux_data->current_chapter );
364                     string = tmp_buffer;
365                 }
366                 mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, mux_data->prev_chapter_tc, string);
367             }
368             mux_data->prev_chapter_tc = timecode;
369         }
370
371         if (job->vcodec == HB_VCODEC_THEORA)
372         {
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)
377             {
378                 hb_error( "Failed to write frame to output file, Disk Full?" );
379                 *job->die = 1;
380             }
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 );
384             return 0;
385         }
386     }
387     else if ( mux_data->subtitle )
388     {
389         uint64_t   duration;
390         timecode = buf->start * TIMECODE_SCALE;
391         if( mk_startFrame(m->file, mux_data->track) < 0)
392         {
393             hb_error( "Failed to write frame to output file, Disk Full?" );
394             *job->die = 1;
395         }
396
397         duration = buf->stop * TIMECODE_SCALE - timecode;
398         if( mux_data->sub_format == TEXTSUB )
399         {
400             mk_addFrameData(m->file, mux_data->track, buf->data, buf->size);
401             mk_setFrameFlags(m->file, mux_data->track, timecode, 1, duration);
402         }
403         else
404         {
405             mk_addFrameData(m->file, mux_data->track, buf->data, buf->size);
406             mk_setFrameFlags(m->file, mux_data->track, timecode, 1, duration);
407         }
408         mk_flushFrame(m->file, mux_data->track);
409         hb_buffer_close( &buf );
410         return 0;
411     }
412     else
413     {
414         /* Audio */
415         timecode = buf->start * TIMECODE_SCALE;
416         if (mux_data->codec == HB_ACODEC_VORBIS)
417         {
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))
422             {
423                 hb_error( "Failed to write frame to output file, Disk Full?" );
424                 *job->die = 1;
425             }
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 );
429             return 0;
430         }
431     }
432
433     if( mk_startFrame(m->file, mux_data->track) < 0)
434     {
435         hb_error( "Failed to write frame to output file, Disk Full?" );
436         *job->die = 1;
437     }
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 );
445     return 0;
446 }
447
448 static int MKVEnd( hb_mux_object_t * m )
449 {
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;
456
457     if( !job->mux_data )
458     {
459         /*
460          * We must have failed to create the file in the first place.
461          */
462         return 0;
463     }
464
465     chapter_data = hb_list_item( title->list_chapter, mux_data->current_chapter++ );
466
467     if(job->chapter_markers)
468     {
469         tmp_buffer[0] = '\0';
470
471         if( chapter_data != NULL )
472         {
473             string = chapter_data->title;
474         }
475
476         if( strlen(string) == 0 || strlen(string) >= 1024 )
477         {
478             snprintf( tmp_buffer, 1023, "Chapter %02i", mux_data->current_chapter );
479             string = tmp_buffer;
480         }
481         mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, mux_data->prev_chapter_tc, string);
482     }
483
484     if( title->metadata )
485     {
486         hb_metadata_t *md = title->metadata;
487
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 );
496     }
497
498     if( mk_close(m->file) < 0 )
499     {
500         hb_error( "Failed to flush the last frame and close the output file, Disk Full?" );
501         *job->die = 1;
502     }
503
504     // TODO: Free what we alloc'd
505
506     return 0;
507 }
508
509 hb_mux_object_t * hb_mux_mkv_init( hb_job_t * job )
510 {
511     hb_mux_object_t * m = calloc( sizeof( hb_mux_object_t ), 1 );
512     m->init      = MKVInit;
513     m->mux       = MKVMux;
514     m->end       = MKVEnd;
515     m->job       = job;
516     return m;
517 }