]> git.sesse.net Git - ffmpeg/blob - libavcodec/dirac_parser.c
avcodec/diracdsp: use av_clip_uintp2
[ffmpeg] / libavcodec / dirac_parser.c
1 /*
2  * Dirac parser
3  *
4  * Copyright (c) 2007-2008 Marco Gerards <marco@gnu.org>
5  * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju@gmail.com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 /**
25  * @file
26  * Dirac Parser
27  * @author Marco Gerards <marco@gnu.org>
28  */
29
30 #include <string.h>
31
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mem.h"
34
35 #include "parser.h"
36
37 #define DIRAC_PARSE_INFO_PREFIX 0x42424344
38
39 /**
40  * Find the end of the current frame in the bitstream.
41  * @return the position of the first byte of the next frame or -1
42  */
43 typedef struct DiracParseContext {
44     int state;
45     int is_synced;
46     int sync_offset;
47     int header_bytes_needed;
48     int overread_index;
49     int buffer_size;
50     int index;
51     uint8_t *buffer;
52     int dirac_unit_size;
53     uint8_t *dirac_unit;
54 } DiracParseContext;
55
56 static int find_frame_end(DiracParseContext *pc,
57                           const uint8_t *buf, int buf_size)
58 {
59     uint32_t state = pc->state;
60     int i = 0;
61
62     if (!pc->is_synced) {
63         for (i = 0; i < buf_size; i++) {
64             state = (state << 8) | buf[i];
65             if (state == DIRAC_PARSE_INFO_PREFIX) {
66                 state                   = -1;
67                 pc->is_synced           = 1;
68                 pc->header_bytes_needed = 9;
69                 pc->sync_offset         = i;
70                 break;
71             }
72         }
73     }
74
75     if (pc->is_synced) {
76         pc->sync_offset = 0;
77         for (; i < buf_size; i++) {
78             if (state == DIRAC_PARSE_INFO_PREFIX) {
79                 if ((buf_size - i) >= pc->header_bytes_needed) {
80                     pc->state = -1;
81                     return i + pc->header_bytes_needed;
82                 } else {
83                     pc->header_bytes_needed = 9 - (buf_size - i);
84                     break;
85                 }
86             } else
87                 state = (state << 8) | buf[i];
88         }
89     }
90     pc->state = state;
91     return -1;
92 }
93
94 typedef struct DiracParseUnit {
95     int next_pu_offset;
96     int prev_pu_offset;
97     uint8_t pu_type;
98 } DiracParseUnit;
99
100 static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc,
101                              int offset)
102 {
103     int i;
104     int8_t *start;
105     static const uint8_t valid_pu_types[] = {
106         0x00, 0x10, 0x20, 0x30, 0x08, 0x48, 0xC8, 0xE8, 0x0A, 0x0C, 0x0D, 0x0E
107     };
108
109     if (offset < 0 || pc->index - 13 < offset)
110         return 0;
111
112     start = pc->buffer + offset;
113     pu->pu_type = start[4];
114
115     pu->next_pu_offset = AV_RB32(start + 5);
116     pu->prev_pu_offset = AV_RB32(start + 9);
117
118     /* Check for valid parse code */
119     for (i = 0; i < 12; i++)
120         if (valid_pu_types[i] == pu->pu_type)
121             break;
122     if (i == 12)
123         return 0;
124
125     if (pu->pu_type == 0x10 && pu->next_pu_offset == 0)
126         pu->next_pu_offset = 13; /* The length of a parse info header */
127
128     /* Check if the parse offsets are somewhat sane */
129     if ((pu->next_pu_offset && pu->next_pu_offset < 13) ||
130         (pu->prev_pu_offset && pu->prev_pu_offset < 13))
131         return 0;
132
133     return 1;
134 }
135
136 static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
137                                int next, const uint8_t **buf, int *buf_size)
138 {
139     int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
140                              s->dts == AV_NOPTS_VALUE);
141     DiracParseContext *pc = s->priv_data;
142
143     if (pc->overread_index) {
144         memmove(pc->buffer, pc->buffer + pc->overread_index,
145                pc->index - pc->overread_index);
146         pc->index         -= pc->overread_index;
147         pc->overread_index = 0;
148         if (*buf_size == 0 && pc->buffer[4] == 0x10) {
149             *buf      = pc->buffer;
150             *buf_size = pc->index;
151             return 0;
152         }
153     }
154
155     if (next == -1) {
156         /* Found a possible frame start but not a frame end */
157         void *new_buffer =
158             av_fast_realloc(pc->buffer, &pc->buffer_size,
159                             pc->index + (*buf_size - pc->sync_offset));
160         if (!new_buffer)
161             return AVERROR(ENOMEM);
162         pc->buffer = new_buffer;
163         memcpy(pc->buffer + pc->index, (*buf + pc->sync_offset),
164                *buf_size - pc->sync_offset);
165         pc->index += *buf_size - pc->sync_offset;
166         return -1;
167     } else {
168         /* Found a possible frame start and a  possible frame end */
169         DiracParseUnit pu1, pu;
170         void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
171                                            pc->index + next);
172         if (!new_buffer)
173             return AVERROR(ENOMEM);
174         pc->buffer = new_buffer;
175         memcpy(pc->buffer + pc->index, *buf, next);
176         pc->index += next;
177
178         /* Need to check if we have a valid Parse Unit. We can't go by the
179          * sync pattern 'BBCD' alone because arithmetic coding of the residual
180          * and motion data can cause the pattern triggering a false start of
181          * frame. So check if the previous parse offset of the next parse unit
182          * is equal to the next parse offset of the current parse unit then
183          * we can be pretty sure that we have a valid parse unit */
184         if (!unpack_parse_unit(&pu1, pc, pc->index - 13)                     ||
185             !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
186             pu.next_pu_offset != pu1.prev_pu_offset                          ||
187             pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset
188         ) {
189             pc->index              -= 9;
190             *buf_size               = next - 9;
191             pc->header_bytes_needed = 9;
192             return -1;
193         }
194
195         /* All non-frame data must be accompanied by frame data. This is to
196          * ensure that pts is set correctly. So if the current parse unit is
197          * not frame data, wait for frame data to come along */
198
199         pc->dirac_unit = pc->buffer + pc->index - 13 -
200                          pu1.prev_pu_offset - pc->dirac_unit_size;
201
202         pc->dirac_unit_size += pu.next_pu_offset;
203
204         if ((pu.pu_type & 0x08) != 0x08) {
205             pc->header_bytes_needed = 9;
206             *buf_size               = next;
207             return -1;
208         }
209
210         /* Get the picture number to set the pts and dts*/
211         if (parse_timing_info && pu1.prev_pu_offset >= 13) {
212             uint8_t *cur_pu = pc->buffer +
213                               pc->index - 13 - pu1.prev_pu_offset;
214             int pts = AV_RB32(cur_pu + 13);
215             if (s->last_pts == 0 && s->last_dts == 0)
216                 s->dts = pts - 1;
217             else
218                 s->dts = s->last_dts + 1;
219             s->pts = pts;
220             if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
221                 avctx->has_b_frames = 1;
222         }
223         if (avctx->has_b_frames && s->pts == s->dts)
224             s->pict_type = AV_PICTURE_TYPE_B;
225
226         /* Finally have a complete Dirac data unit */
227         *buf      = pc->dirac_unit;
228         *buf_size = pc->dirac_unit_size;
229
230         pc->dirac_unit_size     = 0;
231         pc->overread_index      = pc->index - 13;
232         pc->header_bytes_needed = 9;
233     }
234     return next;
235 }
236
237 static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
238                        const uint8_t **poutbuf, int *poutbuf_size,
239                        const uint8_t *buf, int buf_size)
240 {
241     DiracParseContext *pc = s->priv_data;
242     int next;
243
244     *poutbuf      = NULL;
245     *poutbuf_size = 0;
246
247     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
248         next          = buf_size;
249         *poutbuf      = buf;
250         *poutbuf_size = buf_size;
251         /* Assume that data has been packetized into an encapsulation unit. */
252     } else {
253         next = find_frame_end(pc, buf, buf_size);
254         if (!pc->is_synced && next == -1)
255             /* No frame start found yet. So throw away the entire buffer. */
256             return buf_size;
257
258         if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0)
259             return buf_size;
260     }
261
262     *poutbuf      = buf;
263     *poutbuf_size = buf_size;
264     return next;
265 }
266
267 static void dirac_parse_close(AVCodecParserContext *s)
268 {
269     DiracParseContext *pc = s->priv_data;
270
271     if (pc->buffer_size > 0)
272         av_freep(&pc->buffer);
273 }
274
275 AVCodecParser ff_dirac_parser = {
276     .codec_ids      = { AV_CODEC_ID_DIRAC },
277     .priv_data_size = sizeof(DiracParseContext),
278     .parser_parse   = dirac_parse,
279     .parser_close   = dirac_parse_close,
280 };