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 "libavutil/intreadwrite.h"
31#include "parser.h"
32
33#define DIRAC_PARSE_INFO_PREFIX 0x42424344
34
35/**
36 * Finds the end of the current frame in the bitstream.
37 * @return the position of the first byte of the next frame or -1
38 */
39typedef struct DiracParseContext {
40    int state;
41    int is_synced;
42    int sync_offset;
43    int header_bytes_needed;
44    int overread_index;
45    int buffer_size;
46    int index;
47    uint8_t *buffer;
48    int dirac_unit_size;
49    uint8_t *dirac_unit;
50} DiracParseContext;
51
52static int find_frame_end(DiracParseContext *pc,
53                          const uint8_t *buf, int buf_size)
54{
55    uint32_t state = pc->state;
56    int i = 0;
57
58    if (!pc->is_synced) {
59        for (i = 0; i < buf_size; i++) {
60            state = (state << 8) | buf[i];
61            if (state == DIRAC_PARSE_INFO_PREFIX) {
62                state                   = -1;
63                pc->is_synced           = 1;
64                pc->header_bytes_needed = 9;
65                pc->sync_offset         = i;
66                break;
67            }
68        }
69    }
70
71    if (pc->is_synced) {
72        pc->sync_offset = 0;
73        for (; i < buf_size; i++) {
74            if (state == DIRAC_PARSE_INFO_PREFIX) {
75                if ((buf_size-i) >= pc->header_bytes_needed) {
76                    pc->state = -1;
77                    return i + pc->header_bytes_needed;
78                } else {
79                    pc->header_bytes_needed = 9-(buf_size-i);
80                    break;
81                }
82            } else
83              state = (state << 8) | buf[i];
84        }
85    }
86    pc->state = state;
87    return -1;
88}
89
90typedef struct DiracParseUnit
91{
92    int next_pu_offset;
93    int prev_pu_offset;
94    uint8_t pu_type;
95} DiracParseUnit;
96
97static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc,
98                             int offset)
99{
100    uint8_t *start = pc->buffer + offset;
101    uint8_t *end   = pc->buffer + pc->index;
102    if (start < pc->buffer || (start+13 > end))
103        return 0;
104    pu->pu_type = start[4];
105
106    pu->next_pu_offset = AV_RB32(start+5);
107    pu->prev_pu_offset = AV_RB32(start+9);
108
109    if (pu->pu_type == 0x10 && pu->next_pu_offset == 0)
110        pu->next_pu_offset = 13;
111
112    return 1;
113}
114
115static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
116                               int next, const uint8_t **buf, int *buf_size)
117{
118    int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
119                             s->dts == AV_NOPTS_VALUE);
120    DiracParseContext *pc = s->priv_data;
121
122    if (pc->overread_index) {
123        memcpy(pc->buffer, pc->buffer + pc->overread_index,
124               pc->index - pc->overread_index);
125        pc->index -= pc->overread_index;
126        pc->overread_index = 0;
127        if (*buf_size == 0 && pc->buffer[4] == 0x10) {
128            *buf      = pc->buffer;
129            *buf_size = pc->index;
130            return 0;
131        }
132    }
133
134    if ( next == -1) {
135        /* Found a possible frame start but not a frame end */
136        void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
137                                           pc->index + (*buf_size -
138                                                        pc->sync_offset));
139        pc->buffer = new_buffer;
140        memcpy(pc->buffer+pc->index, (*buf + pc->sync_offset),
141               *buf_size - pc->sync_offset);
142        pc->index += *buf_size - pc->sync_offset;
143        return -1;
144    } else {
145        /* Found a possible frame start and a  possible frame end */
146        DiracParseUnit pu1, pu;
147        void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
148                                           pc->index + next);
149        pc->buffer = new_buffer;
150        memcpy(pc->buffer + pc->index, *buf, next);
151        pc->index += next;
152
153        /* Need to check if we have a valid Parse Unit. We can't go by the
154         * sync pattern 'BBCD' alone because arithmetic coding of the residual
155         * and motion data can cause the pattern triggering a false start of
156         * frame. So check if the previous parse offset of the next parse unit
157         * is equal to the next parse offset of the current parse unit then
158         * we can be pretty sure that we have a valid parse unit */
159        if (!unpack_parse_unit(&pu1, pc, pc->index - 13)                     ||
160            !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
161            pu.next_pu_offset != pu1.prev_pu_offset) {
162            pc->index -= 9;
163            *buf_size = next-9;
164            pc->header_bytes_needed = 9;
165            return -1;
166        }
167
168        /* All non-frame data must be accompanied by frame data. This is to
169         * ensure that pts is set correctly. So if the current parse unit is
170         * not frame data, wait for frame data to come along */
171
172        pc->dirac_unit = pc->buffer + pc->index - 13 -
173                         pu1.prev_pu_offset - pc->dirac_unit_size;
174
175        pc->dirac_unit_size += pu.next_pu_offset;
176
177        if ((pu.pu_type&0x08) != 0x08) {
178            pc->header_bytes_needed = 9;
179            *buf_size = next;
180            return -1;
181        }
182
183        /* Get the picture number to set the pts and dts*/
184        if (parse_timing_info) {
185            uint8_t *cur_pu = pc->buffer +
186                              pc->index - 13 - pu1.prev_pu_offset;
187            int pts =  AV_RB32(cur_pu + 13);
188            if (s->last_pts == 0 && s->last_dts == 0)
189                s->dts = pts - 1;
190            else
191                s->dts = s->last_dts+1;
192            s->pts = pts;
193            if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
194                avctx->has_b_frames = 1;
195        }
196        if (avctx->has_b_frames && s->pts == s->dts)
197             s->pict_type = FF_B_TYPE;
198
199        /* Finally have a complete Dirac data unit */
200        *buf      = pc->dirac_unit;
201        *buf_size = pc->dirac_unit_size;
202
203        pc->dirac_unit_size     = 0;
204        pc->overread_index      = pc->index-13;
205        pc->header_bytes_needed = 9;
206    }
207    return next;
208}
209
210static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
211                       const uint8_t **poutbuf, int *poutbuf_size,
212                       const uint8_t *buf, int buf_size)
213{
214    DiracParseContext *pc = s->priv_data;
215    int next;
216
217    *poutbuf = NULL;
218    *poutbuf_size = 0;
219
220    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
221        next = buf_size;
222        *poutbuf = buf;
223        *poutbuf_size = buf_size;
224        /* Assume that data has been packetized into an encapsulation unit. */
225    } else {
226        next = find_frame_end(pc, buf, buf_size);
227        if (!pc->is_synced && next == -1) {
228            /* No frame start found yet. So throw away the entire buffer. */
229            return buf_size;
230        }
231
232        if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0) {
233            return buf_size;
234        }
235    }
236
237    *poutbuf = buf;
238    *poutbuf_size = buf_size;
239    return next;
240}
241
242static void dirac_parse_close(AVCodecParserContext *s)
243{
244    DiracParseContext *pc = s->priv_data;
245
246    if (pc->buffer_size > 0)
247        av_free(pc->buffer);
248}
249
250AVCodecParser dirac_parser = {
251    { CODEC_ID_DIRAC },
252    sizeof(DiracParseContext),
253    NULL,
254    dirac_parse,
255    dirac_parse_close,
256};
257