]> git.sesse.net Git - ffmpeg/blob - libavcodec/dnxhd_parser.c
Merge commit '6aa4ba7131b6e8668e33430e18101a051fe492eb'
[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_find_frame_end(DNXHDParserContext *dctx,
40                                 const uint8_t *buf, int buf_size)
41 {
42     ParseContext *pc = &dctx->pc;
43     uint64_t state = pc->state64;
44     int pic_found = pc->frame_start_found;
45     int i = 0;
46     int interlaced = dctx->interlaced;
47     int cur_field = dctx->cur_field;
48
49     if (!pic_found) {
50         for (i = 0; i < buf_size; i++) {
51             state = (state << 8) | buf[i];
52             if (ff_dnxhd_check_header_prefix(state & 0xffffffffff00LL) != 0) {
53                 i++;
54                 pic_found = 1;
55                 interlaced = (state&2)>>1; /* byte following the 5-byte header prefix */
56                 cur_field = state&1;
57                 dctx->cur_byte = 0;
58                 dctx->remaining = 0;
59                 break;
60             }
61         }
62     }
63
64     if (pic_found && !dctx->remaining) {
65         if (!buf_size) /* EOF considered as end of frame */
66             return 0;
67         for (; i < buf_size; i++) {
68             dctx->cur_byte++;
69             state = (state << 8) | buf[i];
70
71             if (dctx->cur_byte == 24) {
72                 dctx->h = (state >> 32) & 0xFFFF;
73             } else if (dctx->cur_byte == 26) {
74                 dctx->w = (state >> 32) & 0xFFFF;
75             } else if (dctx->cur_byte == 42) {
76                 int cid = (state >> 32) & 0xFFFFFFFF;
77
78                 if (cid <= 0)
79                     continue;
80
81                 dctx->remaining = avpriv_dnxhd_get_frame_size(cid);
82                 if (dctx->remaining <= 0) {
83                     dctx->remaining = ff_dnxhd_get_hr_frame_size(cid, dctx->w, dctx->h);
84                     if (dctx->remaining <= 0)
85                         return dctx->remaining;
86                 }
87                 if (buf_size - i >= dctx->remaining && (!dctx->interlaced || dctx->cur_field)) {
88                     int remaining = dctx->remaining;
89
90                     pc->frame_start_found = 0;
91                     pc->state64 = -1;
92                     dctx->interlaced = interlaced;
93                     dctx->cur_field = 0;
94                     dctx->cur_byte = 0;
95                     dctx->remaining = 0;
96                     return remaining;
97                 } else {
98                     dctx->remaining -= buf_size;
99                 }
100             }
101         }
102     } else if (pic_found) {
103         if (dctx->remaining > buf_size) {
104             dctx->remaining -= buf_size;
105         } else {
106             int remaining = dctx->remaining;
107
108             pc->frame_start_found = 0;
109             pc->state64 = -1;
110             dctx->interlaced = interlaced;
111             dctx->cur_field = 0;
112             dctx->cur_byte = 0;
113             dctx->remaining = 0;
114             return remaining;
115         }
116     }
117     pc->frame_start_found = pic_found;
118     pc->state64 = state;
119     dctx->interlaced = interlaced;
120     dctx->cur_field = cur_field;
121     return END_NOT_FOUND;
122 }
123
124 static int dnxhd_parse(AVCodecParserContext *s,
125                        AVCodecContext *avctx,
126                        const uint8_t **poutbuf, int *poutbuf_size,
127                        const uint8_t *buf, int buf_size)
128 {
129     DNXHDParserContext *dctx = s->priv_data;
130     ParseContext *pc = &dctx->pc;
131     int next;
132
133     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
134         next = buf_size;
135     } else {
136         next = dnxhd_find_frame_end(dctx, buf, buf_size);
137         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
138             *poutbuf      = NULL;
139             *poutbuf_size = 0;
140             return buf_size;
141         }
142     }
143     *poutbuf      = buf;
144     *poutbuf_size = buf_size;
145     return next;
146 }
147
148 AVCodecParser ff_dnxhd_parser = {
149     .codec_ids      = { AV_CODEC_ID_DNXHD },
150     .priv_data_size = sizeof(DNXHDParserContext),
151     .parser_parse   = dnxhd_parse,
152     .parser_close   = ff_parse_close,
153 };