Browse code

Merge commit '90ed6c5cf7f236bc9efb47c97b40358c666d1386'

* commit '90ed6c5cf7f236bc9efb47c97b40358c666d1386':
h2645_parse: compute the actual data length, without trailing paddding

Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>

Derek Buitenhuis authored on 2016/04/26 22:09:52
Showing 2 changed files
... ...
@@ -22,14 +22,13 @@
22 22
 
23 23
 #include "config.h"
24 24
 
25
+#include "libavutil/intmath.h"
25 26
 #include "libavutil/intreadwrite.h"
26 27
 #include "libavutil/mem.h"
27 28
 
28 29
 #include "hevc.h"
29 30
 #include "h2645_parse.h"
30 31
 
31
-/* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
32
- * between these functions would be nice. */
33 32
 int ff_h2645_extract_rbsp(const uint8_t *src, int length,
34 33
                           H2645NAL *nal)
35 34
 {
... ...
@@ -178,6 +177,31 @@ static const char *nal_unit_name(int nal_type)
178 178
     }
179 179
 }
180 180
 
181
+static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
182
+{
183
+    int size = nal->size;
184
+    int v;
185
+
186
+    while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
187
+        size--;
188
+
189
+    if (!size)
190
+        return 0;
191
+
192
+    v = nal->data[size - 1];
193
+
194
+    if (size > INT_MAX / 8)
195
+        return AVERROR(ERANGE);
196
+    size *= 8;
197
+
198
+    /* remove the stop bit and following trailing zeros,
199
+     * or nothing for damaged bitstreams */
200
+    if (v)
201
+        size -= ff_ctz(v) + 1;
202
+
203
+    return size;
204
+}
205
+
181 206
 /**
182 207
  * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
183 208
  * 0 if the unit should be skipped, 1 otherwise
... ...
@@ -231,6 +255,7 @@ int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
231 231
     while (length >= 4) {
232 232
         H2645NAL *nal;
233 233
         int extract_length = 0;
234
+        int skip_trailing_zeros = 1;
234 235
 
235 236
         if (is_nalff) {
236 237
             int i;
... ...
@@ -292,7 +317,15 @@ int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
292 292
 
293 293
         pkt->nb_nals++;
294 294
 
295
-        ret = init_get_bits8(&nal->gb, nal->data, nal->size);
295
+        /* see commit 3566042a0 */
296
+        if (consumed < length - 3 &&
297
+            buf[consumed]     == 0x00 && buf[consumed + 1] == 0x00 &&
298
+            buf[consumed + 2] == 0x01 && buf[consumed + 3] == 0xE0)
299
+            skip_trailing_zeros = 0;
300
+
301
+        nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
302
+
303
+        ret = init_get_bits8(&nal->gb, nal->data, nal->size_bits);
296 304
         if (ret < 0)
297 305
             return ret;
298 306
 
... ...
@@ -33,6 +33,12 @@ typedef struct H2645NAL {
33 33
     int size;
34 34
     const uint8_t *data;
35 35
 
36
+    /**
37
+     * Size, in bits, of just the data, excluding the stop bit and any trailing
38
+     * padding. I.e. what HEVC calls SODB.
39
+     */
40
+    int size_bits;
41
+
36 42
     int raw_size;
37 43
     const uint8_t *raw_data;
38 44