]> granicus.if.org Git - handbrake/commitdiff
encca_aac: flush all the remaining packets from encoders
authorDamiano Galassi <damiog@gmail.com>
Fri, 24 Feb 2017 18:13:41 +0000 (19:13 +0100)
committerDamiano Galassi <galad87@users.noreply.github.com>
Fri, 24 Feb 2017 18:23:03 +0000 (19:23 +0100)
libhb/platform/macosx/encca_aac.c

index 422c59bdbeb30454ea374396109d268416f86083..052eadb4b717aae6a4112d7d523d3916a610514d 100644 (file)
@@ -51,6 +51,8 @@ struct hb_work_private_s
     uint64_t samples, ibytes;
     Float64 osamplerate;
 
+    int input_done;
+
     hb_audio_remap_t *remap;
 };
 
@@ -377,7 +379,17 @@ static OSStatus inInputDataProc(AudioConverterRef converter, UInt32 *npackets,
     if (!pv->ibytes)
     {
         *npackets = 0;
-        return 1;
+        if (pv->input_done)
+        {
+            // EOF on input
+            buffers->mBuffers[0].mDataByteSize = 0;
+            return noErr;
+        }
+        else
+        {
+            // Not enough data available
+            return 1;
+        }
     }
 
     if (pv->buf != NULL)
@@ -420,8 +432,12 @@ static hb_buffer_t* Encode(hb_work_object_t *w)
     hb_work_private_t *pv = w->private_data;
     UInt32 npackets = 1;
 
-    /* check if we need more data */
-    if ((pv->ibytes = hb_list_bytes(pv->list)) < pv->isamples * pv->isamplesiz)
+    /* check if we need more data or we have already got to EOF
+       if so, we need to call the audio converter again even
+       without data to get out the remaining packets.
+     */
+    if (pv->input_done != 1 &&
+        (pv->ibytes = hb_list_bytes(pv->list)) < pv->isamples * pv->isamplesiz)
     {
         return NULL;
     }
@@ -449,7 +465,6 @@ static hb_buffer_t* Encode(hb_work_object_t *w)
     // only drop the output buffer if it's actually empty
     if (!npackets || odesc.mDataByteSize <= 0)
     {
-        hb_log("encCoreAudio: 0 packets returned");
         return NULL;
     }
 
@@ -480,23 +495,20 @@ static hb_buffer_t* Flush(hb_work_object_t *w, hb_buffer_t *bufin)
         hb_list_add(pv->list, tmp);
     }
 
-    hb_buffer_t *bufout = NULL, *buf = NULL;
-    while (hb_list_bytes(pv->list) >= pv->isamples * pv->isamplesiz)
+    hb_buffer_t *bufout = NULL, *buf = NULL, *b = NULL;
+    while ((b = Encode(w)))
     {
-        hb_buffer_t *b = Encode(w);
-        if (b != NULL)
+        if (bufout == NULL)
         {
-            if (bufout == NULL)
-            {
-                bufout = b;
-            }
-            else
-            {
-                buf->next = b;
-            }
-            buf = b;
+            bufout = b;
         }
+        else
+        {
+            buf->next = b;
+        }
+        buf = b;
     }
+
     // add the eof marker to the end of our buf chain
     if (buf != NULL)
     {
@@ -526,6 +538,7 @@ int encCoreAudioWork(hb_work_object_t *w, hb_buffer_t **buf_in,
     {
         // EOF on input. Finish encoding what we have buffered then send
         // it & the eof downstream.
+        pv->input_done = 1;
         *buf_out = Flush(w, in);
         return HB_WORK_DONE;
     }