Browse code

libavcodec/dnxhd_parser: add parser and probe support raw 444 and dnxhr formats

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>

Mark Reid authored on 2016/02/14 14:44:32
Showing 5 changed files
... ...
@@ -25,8 +25,7 @@
25 25
  */
26 26
 
27 27
 #include "parser.h"
28
-
29
-#define DNXHD_HEADER_PREFIX 0x000002800100
28
+#include "dnxhddata.h"
30 29
 
31 30
 typedef struct {
32 31
     ParseContext pc;
... ...
@@ -47,7 +46,7 @@ static int dnxhd_find_frame_end(DNXHDParserContext *dctx,
47 47
     if (!pic_found) {
48 48
         for (i = 0; i < buf_size; i++) {
49 49
             state = (state << 8) | buf[i];
50
-            if ((state & 0xffffffffff00LL) == DNXHD_HEADER_PREFIX) {
50
+            if (ff_dnxhd_check_header_prefix(state & 0xffffffffff00LL) != 0) {
51 51
                 i++;
52 52
                 pic_found = 1;
53 53
                 interlaced = (state&2)>>1; /* byte following the 5-byte header prefix */
... ...
@@ -62,7 +61,7 @@ static int dnxhd_find_frame_end(DNXHDParserContext *dctx,
62 62
             return 0;
63 63
         for (; i < buf_size; i++) {
64 64
             state = (state << 8) | buf[i];
65
-            if ((state & 0xffffffffff00LL) == DNXHD_HEADER_PREFIX) {
65
+            if (ff_dnxhd_check_header_prefix(state & 0xffffffffff00LL) != 0) {
66 66
                 if (!interlaced || dctx->cur_field) {
67 67
                     pc->frame_start_found = 0;
68 68
                     pc->state64 = -1;
... ...
@@ -22,6 +22,7 @@
22 22
 #include "avcodec.h"
23 23
 #include "dnxhddata.h"
24 24
 #include "libavutil/common.h"
25
+#include "libavutil/intreadwrite.h"
25 26
 
26 27
 /* The quantization tables below are in zigzag order! */
27 28
 
... ...
@@ -1102,6 +1103,13 @@ int avpriv_dnxhd_get_interlaced(int cid)
1102 1102
     return ff_dnxhd_cid_table[i].flags & DNXHD_INTERLACED ? 1 : 0;
1103 1103
 }
1104 1104
 
1105
+uint64_t avpriv_dnxhd_parse_header_prefix(const uint8_t *buf)
1106
+{
1107
+    uint64_t prefix = AV_RB32(buf);
1108
+    prefix = (prefix << 16) | buf[4] << 8;
1109
+    return ff_dnxhd_check_header_prefix(prefix);
1110
+}
1111
+
1105 1112
 int ff_dnxhd_find_cid(AVCodecContext *avctx, int bit_depth)
1106 1113
 {
1107 1114
     int i, j;
... ...
@@ -31,6 +31,12 @@
31 31
 #define DNXHD_MBAFF        (1<<1)
32 32
 #define DNXHD_444          (1<<2)
33 33
 
34
+/** Frame headers, extra 0x00 added to end for parser */
35
+#define DNXHD_HEADER_INITIAL 0x000002800100
36
+#define DNXHD_HEADER_444     0x000002800200
37
+#define DNXHD_HEADER_HR1     0x000002800300
38
+#define DNXHD_HEADER_HR2     0x0000038C0300
39
+
34 40
 /** Indicate that a CIDEntry value must be read in the bitstream */
35 41
 #define DNXHD_VARIABLE 0
36 42
 
... ...
@@ -59,7 +65,17 @@ int ff_dnxhd_get_cid_table(int cid);
59 59
 int ff_dnxhd_find_cid(AVCodecContext *avctx, int bit_depth);
60 60
 void ff_dnxhd_print_profiles(AVCodecContext *avctx, int loglevel);
61 61
 
62
+static av_always_inline uint64_t ff_dnxhd_check_header_prefix(uint64_t prefix)
63
+{
64
+    if (prefix == DNXHD_HEADER_INITIAL ||
65
+        prefix == DNXHD_HEADER_444     ||
66
+        prefix == DNXHD_HEADER_HR1     ||
67
+        prefix == DNXHD_HEADER_HR2)
68
+        return prefix;
69
+    return 0;
70
+}
71
+
62 72
 int avpriv_dnxhd_get_frame_size(int cid);
63 73
 int avpriv_dnxhd_get_interlaced(int cid);
64
-
74
+uint64_t avpriv_dnxhd_parse_header_prefix(const uint8_t *buf);
65 75
 #endif /* AVCODEC_DNXHDDATA_H */
... ...
@@ -163,21 +163,17 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
163 163
                                const uint8_t *buf, int buf_size,
164 164
                                int first_field)
165 165
 {
166
-    static const uint8_t header_prefix[]    = { 0x00, 0x00, 0x02, 0x80, 0x01 };
167
-    static const uint8_t header_prefix444[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };
168
-    static const uint8_t header_prefixhr1[] = { 0x00, 0x00, 0x02, 0x80, 0x03 };
169
-    static const uint8_t header_prefixhr2[] = { 0x00, 0x00, 0x03, 0x8C, 0x03 };
170 166
     int i, cid, ret;
171 167
     int old_bit_depth = ctx->bit_depth, bitdepth;
172
-
168
+    uint64_t header_prefix;
173 169
     if (buf_size < 0x280) {
174 170
         av_log(ctx->avctx, AV_LOG_ERROR,
175 171
                "buffer too small (%d < 640).\n", buf_size);
176 172
         return AVERROR_INVALIDDATA;
177 173
     }
178 174
 
179
-    if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5) &&
180
-        memcmp(buf, header_prefixhr1, 5) && memcmp(buf, header_prefixhr2, 5)) {
175
+    header_prefix = avpriv_dnxhd_parse_header_prefix(buf);
176
+    if (header_prefix == 0) {
181 177
         av_log(ctx->avctx, AV_LOG_ERROR,
182 178
                "unknown header 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
183 179
                buf[0], buf[1], buf[2], buf[3], buf[4]);
... ...
@@ -279,7 +275,7 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
279 279
            ctx->bit_depth, ctx->mbaff, ctx->act);
280 280
 
281 281
     // Newer format supports variable mb_scan_index sizes
282
-    if (!memcmp(buf, header_prefixhr2, 5)) {
282
+    if (header_prefix == DNXHD_HEADER_HR2) {
283 283
         ctx->data_offset = 0x170 + (ctx->mb_height << 2);
284 284
     } else {
285 285
         if (ctx->mb_height > 68 ||
... ...
@@ -23,21 +23,22 @@
23 23
 #include "libavutil/intreadwrite.h"
24 24
 #include "avformat.h"
25 25
 #include "rawdec.h"
26
+#include "libavcodec/dnxhddata.h"
26 27
 
27 28
 static int dnxhd_probe(AVProbeData *p)
28 29
 {
29
-    static const uint8_t header[] = {0x00,0x00,0x02,0x80,0x01};
30 30
     int w, h, compression_id;
31 31
     if (p->buf_size < 0x2c)
32 32
         return 0;
33
-    if (memcmp(p->buf, header, 5))
33
+    if (avpriv_dnxhd_parse_header_prefix(p->buf) == 0)
34 34
         return 0;
35 35
     h = AV_RB16(p->buf + 0x18);
36 36
     w = AV_RB16(p->buf + 0x1a);
37 37
     if (!w || !h)
38 38
         return 0;
39 39
     compression_id = AV_RB32(p->buf + 0x28);
40
-    if (compression_id < 1235 || compression_id > 1260)
40
+    if ((compression_id < 1235 || compression_id > 1260) &&
41
+        (compression_id < 1270 || compression_id > 1274))
41 42
         return 0;
42 43
     return AVPROBE_SCORE_MAX;
43 44
 }