OSDN Git Service

Repeat after me, eddyg is a wally.
[handbrake-jp/handbrake-jp-git.git] / libhb / muxmkv.c
index 653fe74..b7c7d2d 100644 (file)
 
 #include "hb.h"
 
+/* Scale factor to apply to timecodes to convert from HandBrake's
+ * 1/90000s to nanoseconds as expected by libmkv */
+#define TIMECODE_SCALE 1000000000 / 90000
+
 struct hb_mux_object_s
 {
     HB_MUX_COMMON;
@@ -24,7 +28,6 @@ struct hb_mux_data_s
 {
     mk_Track  * track;
     uint64_t  prev_chapter_tc;
-    uint64_t  max_tc;
     uint16_t  current_chapter;
 };
 
@@ -50,12 +53,21 @@ static int MKVInit( hb_mux_object_t * m )
 
     m->file = mk_createWriter(job->file, 1000000, 1);
 
+    if( !m->file )
+    {
+        hb_error( "Could not create output file, Disk Full?" );
+        job->mux_data = NULL;
+        *job->die = 1;
+        return 0;
+    }
+
     /* Video track */
     mux_data      = calloc(1, sizeof( hb_mux_data_t ) );
     job->mux_data = mux_data;
 
     track->trackType = MK_TRACK_VIDEO;
     track->flagDefault = 1;
+    track->flagEnabled = 1;
     switch (job->vcodec)
     {
         case HB_VCODEC_X264:
@@ -85,6 +97,8 @@ static int MKVInit( hb_mux_object_t * m )
             memcpy( avcC+11+job->config.h264.sps_length, job->config.h264.pps, job->config.h264.pps_length );
             track->codecPrivate = avcC;
             track->codecPrivateSize = avcC_len;
+            if (job->areBframes)
+                track->minCache = 1;
             break;
         case HB_VCODEC_XVID:
         case HB_VCODEC_FFMPEG:
@@ -94,7 +108,7 @@ static int MKVInit( hb_mux_object_t * m )
             break;
         default:
             *job->die = 1;
-            hb_log("muxmkv: Unknown video codec: %x", job->vcodec);
+            hb_error("muxmkv: Unknown video codec: %x", job->vcodec);
             return 0;
     }
 
@@ -138,35 +152,24 @@ static int MKVInit( hb_mux_object_t * m )
                 break;
             case HB_ACODEC_VORBIS:
                 {
-                    int i, j;
-                    int64_t offset = 0;
-                    int64_t cp_size = 0;
-                    char    *cp;
+                    int i;
+                    uint64_t cp_size = 0;
                     track->codecID = MK_ACODEC_VORBIS;
-                    cp_size = sizeof( char );
+                    uint64_t  header_sizes[3];
                     for (i = 0; i < 3; ++i)
                     {
                         ogg_headers[i] = (ogg_packet *)audio->config.vorbis.headers[i];
                         ogg_headers[i]->packet = (unsigned char *)&audio->config.vorbis.headers[i] + sizeof( ogg_packet );
-                        cp_size += (sizeof( char ) * ((ogg_headers[i]->bytes / 255) + 1)) + ogg_headers[i]->bytes;
-                            /* This will be too big, but it doesn't matter, as we only need it to be big enough. */
-                    }
-                    cp = track->codecPrivate = calloc(1, cp_size);
-                    cp[offset++] = 0x02;
-                    for (i = 0; i < 2; ++i)
-                    {
-                        for (j = ogg_headers[i]->bytes; j >= 255; j -= 255)
-                        {
-                            cp[offset++] = 255;
-                        }
-                        cp[offset++] = j;
+                        header_sizes[i] = ogg_headers[i]->bytes;
                     }
+                    track->codecPrivate = mk_laceXiph(header_sizes, 2, &cp_size);
+                    track->codecPrivate = realloc(track->codecPrivate, cp_size + ogg_headers[0]->bytes + ogg_headers[1]->bytes + ogg_headers[2]->bytes);
                     for(i = 0; i < 3; ++i)
                     {
-                        memcpy(cp + offset, ogg_headers[i]->packet, ogg_headers[i]->bytes);
-                        offset += ogg_headers[i]->bytes;
+                        memcpy(track->codecPrivate + cp_size, ogg_headers[i]->packet, ogg_headers[i]->bytes);
+                        cp_size += ogg_headers[i]->bytes;
                     }
-                    track->codecPrivateSize = offset;
+                    track->codecPrivateSize = cp_size;
                 }
                 break;
             case HB_ACODEC_FAAC:
@@ -176,7 +179,7 @@ static int MKVInit( hb_mux_object_t * m )
                 break;
             default:
                 *job->die = 1;
-                hb_log("muxmkv: Unknown audio codec: %x", job->acodec);
+                hb_error("muxmkv: Unknown audio codec: %x", job->acodec);
                 return 0;
         }
         
@@ -185,18 +188,22 @@ static int MKVInit( hb_mux_object_t * m )
             track->flagDefault = 1;
             default_track_flag = 0;
         }
-        
+        track->flagEnabled = 1;
         track->trackType = MK_TRACK_AUDIO;
         track->language = audio->iso639_2;
         track->audio.samplingFreq = (float)job->arate;
         track->audio.channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->amixdown);
 //        track->defaultDuration = job->arate * 1000;
         mux_data->track = mk_createTrack(m->file, track);
-        if (track->codecPrivate != NULL)
+        if (job->acodec == HB_ACODEC_VORBIS && track->codecPrivate != NULL)
           free(track->codecPrivate);
     }
 
-    mk_writeHeader( m->file, "HandBrake " HB_VERSION);
+    if( mk_writeHeader( m->file, "HandBrake " HB_VERSION) < 0 )
+    {
+        hb_error( "Failed to write to output file, disk full?");
+        *job->die = 1;
+    }
     if (track != NULL)
         free(track);
     if (avcC != NULL)
@@ -217,16 +224,13 @@ static int MKVMux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
     if (mux_data == job->mux_data)
     {
         /* Video */
-        /* Where does the 11130 come from? I had to calculate it from the actual
-          * and the observed duration of the file. Otherwise the timecodes come
-          * out way too small, and you get a 2hr movie that plays in .64 sec.  */
         if ((job->vcodec == HB_VCODEC_X264) && (buf->frametype & HB_FRAME_REF))
         {
-            timecode = (buf->start + (buf->renderOffset - 1000000)) * 11130;
+            timecode = (buf->start + (buf->renderOffset - 1000000)) * TIMECODE_SCALE;
         }
         else
         {
-            timecode = buf->start * 11130;
+            timecode = buf->start * TIMECODE_SCALE;
         }
 
         if (job->chapter_markers && (buf->new_chap || timecode == 0))
@@ -234,7 +238,7 @@ static int MKVMux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
             /* Make sure we're not writing a chapter that has 0 length */
             if (mux_data->prev_chapter_tc != timecode)
             {
-                chapter_data = hb_list_item( title->list_chapter, mux_data->current_chapter );
+                chapter_data = hb_list_item( title->list_chapter, mux_data->current_chapter++ );
                 tmp_buffer[0] = '\0';
 
                 if( chapter_data != NULL )
@@ -244,21 +248,18 @@ static int MKVMux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
 
                 if( strlen(string) == 0 || strlen(string) >= 1024 )
                 {
-                    snprintf( tmp_buffer, 1023, "Chapter %02i", mux_data->current_chapter++ );
+                    snprintf( tmp_buffer, 1023, "Chapter %02i", mux_data->current_chapter );
                     string = tmp_buffer;
                 }
-                mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, timecode, string);
+                mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, mux_data->prev_chapter_tc, string);
             }
             mux_data->prev_chapter_tc = timecode;
         }
-
-        if (buf->stop * 11130 > mux_data->max_tc)
-            mux_data->max_tc = buf->stop * 11130;
     }
     else
     {
         /* Audio */
-        timecode = buf->start * 11130;
+        timecode = buf->start * TIMECODE_SCALE;
         if (job->acodec == HB_ACODEC_VORBIS)
         {
             /* ughhh, vorbis is a pain :( */
@@ -273,10 +274,14 @@ static int MKVMux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
         }
     }
 
-    mk_startFrame(m->file, mux_data->track);
+    if( mk_startFrame(m->file, mux_data->track) < 0)
+    {
+        hb_error( "Failed to write frame to output file, Disk Full?" );
+        *job->die = 1;
+    }
     mk_addFrameData(m->file, mux_data->track, buf->data, buf->size);
     mk_setFrameFlags(m->file, mux_data->track, timecode,
-        ((job->vcodec == HB_VCODEC_X264 && mux_data == job->mux_data) ? (buf->frametype == HB_FRAME_IDR) : ((buf->frametype & HB_FRAME_KEY) != 0)) );
+                     ((job->vcodec == HB_VCODEC_X264 && mux_data == job->mux_data) ? (buf->frametype == HB_FRAME_IDR) : ((buf->frametype & HB_FRAME_KEY) != 0)) );
     return 0;
 }
 
@@ -285,10 +290,20 @@ static int MKVEnd( hb_mux_object_t * m )
     hb_job_t  *job = m->job;
     hb_mux_data_t *mux_data = job->mux_data;
     hb_title_t  *title = job->title;
-    hb_chapter_t *chapter_data = hb_list_item( title->list_chapter, mux_data->current_chapter );
+    hb_chapter_t *chapter_data;
     char tmp_buffer[1024];
     char *string = tmp_buffer;
 
+    if( !job->mux_data )
+    {
+        /*
+         * We must have failed to create the file in the first place.
+         */
+        return 0;
+    }
+
+    chapter_data = hb_list_item( title->list_chapter, mux_data->current_chapter++ );
+
     if(job->chapter_markers)
     {
         tmp_buffer[0] = '\0';
@@ -303,10 +318,14 @@ static int MKVEnd( hb_mux_object_t * m )
             snprintf( tmp_buffer, 1023, "Chapter %02i", mux_data->current_chapter );
             string = tmp_buffer;
         }
-        mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, mux_data->max_tc, string);
+        mk_createChapterSimple(m->file, mux_data->prev_chapter_tc, mux_data->prev_chapter_tc, string);
     }
 
-    mk_close(m->file);
+    if( mk_close(m->file) < 0 )
+    {
+        hb_error( "Failed to flush the last frame and close the output file, Disk Full?" );
+        *job->die = 1;
+    }
 
     // TODO: Free what we alloc'd