]> git.sesse.net Git - ffmpeg/blob - libavcodec/dnxhd_parser.c
Merge commit 'f22da2cdf90dc892d483e2d4003cffc0500816f6'
[ffmpeg] / libavcodec / dnxhd_parser.c
1 /*
2  * DNxHD/VC-3 parser
3  * Copyright (c) 2008 Baptiste Coudurier <baptiste.coudurier@free.fr>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * DNxHD/VC-3 parser
25  */
26
27 #include "parser.h"
28 #include "dnxhddata.h"
29
30 typedef struct {
31     ParseContext pc;
32     int interlaced;
33     int cur_field; /* first field is 0, second is 1 */
34     int cur_byte;
35     int remaining;
36     int w, h;
37 } DNXHDParserContext;
38
39 static int dnxhd_get_hr_frame_size(int cid, int w, int h)
40 {
41     int result, i = ff_dnxhd_get_cid_table(cid);
42
43     if (i < 0)
44         return i;
45
46     result = ((h + 15) / 16) * ((w + 15) / 16) * (int64_t)ff_dnxhd_cid_table[i].packet_scale.num / ff_dnxhd_cid_table[i].packet_scale.den;
47     result = (result + 2048) / 4096 * 4096;
48
49     return FFMAX(result, 8192);
50 }
51
52 static int dnxhd_find_frame_end(DNXHDParserContext *dctx,
53                                 const uint8_t *buf, int buf_size)
54 {
55     ParseContext *pc = &dctx->pc;
56     uint64_t state = pc->state64;
57     int pic_found = pc->frame_start_found;
58     int i = 0;
59     int interlaced = dctx->interlaced;
60     int cur_field = dctx->cur_field;
61
62     if (!pic_found) {
63         for (i = 0; i < buf_size; i++) {
64             state = (state << 8) | buf[i];
65             if (ff_dnxhd_check_header_prefix(state & 0xffffffffff00LL) != 0) {
66                 i++;
67                 pic_found = 1;
68                 interlaced = (state&2)>>1; /* byte following the 5-byte header prefix */
69                 cur_field = state&1;
70                 dctx->cur_byte = 0;
71                 dctx->remaining = 0;
72                 break;
73             }
74         }
75     }
76
77     if (pic_found && !dctx->remaining) {
78         if (!buf_size) /* EOF considered as end of frame */
79             return 0;
80         for (; i < buf_size; i++) {
81             dctx->cur_byte++;
82             state = (state << 8) | buf[i];
83
84             if (dctx->cur_byte == 24) {
85                 dctx->h = (state >> 32) & 0xFFFF;
86             } else if (dctx->cur_byte == 26) {
87                 dctx->w = (state >> 32) & 0xFFFF;
88             } else if (dctx->cur_byte == 42) {
89                 int cid = (state >> 32) & 0xFFFFFFFF;
90
91                 if (cid <= 0)
92                     continue;
93
94                 dctx->remaining = avpriv_dnxhd_get_frame_size(cid);
95                 if (dctx->remaining <= 0) {
96                     dctx->remaining = dnxhd_get_hr_frame_size(cid, dctx->w, dctx->h);
97                     if (dctx->remaining <= 0)
98                         return dctx->remaining;
99                 }
100                 if (buf_size - i >= dctx->remaining && (!dctx->interlaced || dctx->cur_field)) {
101                     int remaining = dctx->remaining;
102
103                     pc->frame_start_found = 0;
104                     pc->state64 = -1;
105                     dctx->interlaced = interlaced;
106                     dctx->cur_field = 0;
107                     dctx->cur_byte = 0;
108                     dctx->remaining = 0;
109                     return remaining;
110                 } else {
111                     dctx->remaining -= buf_size;
112                 }
113             }
114         }
115     } else if (pic_found) {
116         if (dctx->remaining > buf_size) {
117             dctx->remaining -= buf_size;
118         } else {
119             int remaining = dctx->remaining;
120
121             pc->frame_start_found = 0;
122             pc->state64 = -1;
123             dctx->interlaced = interlaced;
124             dctx->cur_field = 0;
125             dctx->cur_byte = 0;
126             dctx->remaining = 0;
127             return remaining;
128         }
129     }
130     pc->frame_start_found = pic_found;
131     pc->state64 = state;
132     dctx->interlaced = interlaced;
133     dctx->cur_field = cur_field;
134     return END_NOT_FOUND;
135 }
136
137 static int dnxhd_parse(AVCodecParserContext *s,
138                        AVCodecContext *avctx,
139                        const uint8_t **poutbuf, int *poutbuf_size,
140                        const uint8_t *buf, int buf_size)
141 {
142     DNXHDParserContext *dctx = s->priv_data;
143     ParseContext *pc = &dctx->pc;
144     int next;
145
146     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
147         next = buf_size;
148     } else {
149         next = dnxhd_find_frame_end(dctx, buf, buf_size);
150         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
151             *poutbuf      = NULL;
152             *poutbuf_size = 0;
153             return buf_size;
154         }
155     }
156     *poutbuf      = buf;
157     *poutbuf_size = buf_size;
158     return next;
159 }
160
161 AVCodecParser ff_dnxhd_parser = {
162     .codec_ids      = { AV_CODEC_ID_DNXHD },
163     .priv_data_size = sizeof(DNXHDParserContext),
164     .parser_parse   = dnxhd_parse,
165     .parser_close   = ff_parse_close,
166 };