Coverage Report

Created: 2024-09-06 07:53

/src/ffmpeg/libavcodec/dirac_parser.c
Line
Count
Source (jump to first uncovered line)
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
37.6M
#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
1.37M
{
59
1.37M
    uint32_t state = pc->state;
60
1.37M
    int i = 0;
61
62
1.37M
    if (!pc->is_synced) {
63
6.51M
        for (i = 0; i < buf_size; i++) {
64
6.49M
            state = (state << 8) | buf[i];
65
6.49M
            if (state == DIRAC_PARSE_INFO_PREFIX) {
66
2.70k
                state                   = -1;
67
2.70k
                pc->is_synced           = 1;
68
2.70k
                pc->header_bytes_needed = 9;
69
2.70k
                pc->sync_offset         = i;
70
2.70k
                break;
71
2.70k
            }
72
6.49M
        }
73
21.5k
    }
74
75
1.37M
    if (pc->is_synced) {
76
1.36M
        pc->sync_offset = 0;
77
31.2M
        for (; i < buf_size; i++) {
78
31.1M
            if (state == DIRAC_PARSE_INFO_PREFIX) {
79
1.25M
                if ((buf_size - i) >= pc->header_bytes_needed) {
80
1.14M
                    pc->state = -1;
81
1.14M
                    return i + pc->header_bytes_needed;
82
1.14M
                } else {
83
105k
                    pc->header_bytes_needed = 9 - (buf_size - i);
84
105k
                    break;
85
105k
                }
86
1.25M
            } else
87
29.8M
                state = (state << 8) | buf[i];
88
31.1M
        }
89
1.36M
    }
90
233k
    pc->state = state;
91
233k
    return -1;
92
1.37M
}
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
2.16M
{
103
2.16M
    int i;
104
2.16M
    int8_t *start;
105
2.16M
    static const uint8_t valid_pu_types[] = {
106
2.16M
        0x00, 0x10, 0x20, 0x30, 0x08, 0x48, 0xC8, 0xE8, 0x0A, 0x0C, 0x0D, 0x0E,
107
2.16M
        0x4C, 0x09, 0xCC, 0x88, 0xCB
108
2.16M
    };
109
110
2.16M
    if (offset < 0 || pc->index - 13 < offset)
111
122k
        return 0;
112
113
2.03M
    start = pc->buffer + offset;
114
2.03M
    pu->pu_type = start[4];
115
116
2.03M
    pu->next_pu_offset = AV_RB32(start + 5);
117
2.03M
    pu->prev_pu_offset = AV_RB32(start + 9);
118
119
    /* Check for valid parse code */
120
11.1M
    for (i = 0; i < 17; i++)
121
11.0M
        if (valid_pu_types[i] == pu->pu_type)
122
1.85M
            break;
123
2.03M
    if (i == 17)
124
183k
        return 0;
125
126
1.85M
    if (pu->pu_type == 0x10 && pu->next_pu_offset == 0x00)
127
242k
        pu->next_pu_offset = 13; /* The length of a parse info header */
128
129
    /* Check if the parse offsets are somewhat sane */
130
1.85M
    if ((pu->next_pu_offset && pu->next_pu_offset < 13) ||
131
1.85M
        (pu->prev_pu_offset && pu->prev_pu_offset < 13))
132
57.8k
        return 0;
133
134
1.79M
    return 1;
135
1.85M
}
136
137
static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
138
                               int next, const uint8_t **buf, int *buf_size)
139
1.36M
{
140
1.36M
    int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
141
1.36M
                             s->dts == AV_NOPTS_VALUE);
142
1.36M
    DiracParseContext *pc = s->priv_data;
143
144
1.36M
    if (pc->overread_index) {
145
492k
        memmove(pc->buffer, pc->buffer + pc->overread_index,
146
492k
               pc->index - pc->overread_index);
147
492k
        pc->index         -= pc->overread_index;
148
492k
        pc->overread_index = 0;
149
492k
        if (*buf_size == 0 && pc->buffer[4] == 0x10) {
150
698
            *buf      = pc->buffer;
151
698
            *buf_size = pc->index;
152
698
            return 0;
153
698
        }
154
492k
    }
155
156
1.35M
    if (next == -1) {
157
        /* Found a possible frame start but not a frame end */
158
213k
        void *new_buffer =
159
213k
            av_fast_realloc(pc->buffer, &pc->buffer_size,
160
213k
                            pc->index + (*buf_size - pc->sync_offset));
161
213k
        if (!new_buffer)
162
0
            return AVERROR(ENOMEM);
163
213k
        pc->buffer = new_buffer;
164
213k
        memcpy(pc->buffer + pc->index, (*buf + pc->sync_offset),
165
213k
               *buf_size - pc->sync_offset);
166
213k
        pc->index += *buf_size - pc->sync_offset;
167
213k
        return -1;
168
1.14M
    } else {
169
        /* Found a possible frame start and a  possible frame end */
170
1.14M
        DiracParseUnit pu1, pu;
171
1.14M
        void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
172
1.14M
                                           pc->index + next);
173
1.14M
        if (!new_buffer)
174
0
            return AVERROR(ENOMEM);
175
1.14M
        pc->buffer = new_buffer;
176
1.14M
        memcpy(pc->buffer + pc->index, *buf, next);
177
1.14M
        pc->index += next;
178
179
        /* Need to check if we have a valid Parse Unit. We can't go by the
180
         * sync pattern 'BBCD' alone because arithmetic coding of the residual
181
         * and motion data can cause the pattern triggering a false start of
182
         * frame. So check if the previous parse offset of the next parse unit
183
         * is equal to the next parse offset of the current parse unit then
184
         * we can be pretty sure that we have a valid parse unit */
185
1.14M
        if (!unpack_parse_unit(&pu1, pc, pc->index - 13)                     ||
186
1.14M
            !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
187
1.14M
            pu.next_pu_offset != pu1.prev_pu_offset                          ||
188
1.14M
            pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset
189
1.14M
        ) {
190
442k
            pc->index              -= 9;
191
442k
            *buf_size               = next - 9;
192
442k
            pc->header_bytes_needed = 9;
193
442k
            return -1;
194
442k
        }
195
196
        /* All non-frame data must be accompanied by frame data. This is to
197
         * ensure that pts is set correctly. So if the current parse unit is
198
         * not frame data, wait for frame data to come along */
199
200
703k
        pc->dirac_unit = pc->buffer + pc->index - 13 -
201
703k
                         pu1.prev_pu_offset - pc->dirac_unit_size;
202
203
703k
        pc->dirac_unit_size += pu.next_pu_offset;
204
205
703k
        if ((pu.pu_type & 0x08) != 0x08) {
206
210k
            pc->header_bytes_needed = 9;
207
210k
            *buf_size               = next;
208
210k
            return -1;
209
210k
        }
210
211
        /* Get the picture number to set the pts and dts*/
212
492k
        if (parse_timing_info && pu1.prev_pu_offset >= 13) {
213
385k
            uint8_t *cur_pu = pc->buffer +
214
385k
                              pc->index - 13 - pu1.prev_pu_offset;
215
385k
            int64_t pts = AV_RB32(cur_pu + 13);
216
385k
            if (s->last_pts == 0 && s->last_dts == 0)
217
12.4k
                s->dts = pts - 1;
218
373k
            else if (s->last_dts != AV_NOPTS_VALUE)
219
358k
                s->dts = s->last_dts + 1;
220
385k
            s->pts = pts;
221
385k
            if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
222
1.43k
                avctx->has_b_frames = 1;
223
385k
        }
224
492k
        if (avctx->has_b_frames && s->pts == s->dts)
225
55.7k
            s->pict_type = AV_PICTURE_TYPE_B;
226
227
        /* Finally have a complete Dirac data unit */
228
492k
        *buf      = pc->dirac_unit;
229
492k
        *buf_size = pc->dirac_unit_size;
230
231
492k
        pc->dirac_unit_size     = 0;
232
492k
        pc->overread_index      = pc->index - 13;
233
492k
        pc->header_bytes_needed = 9;
234
492k
    }
235
492k
    return next;
236
1.35M
}
237
238
static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
239
                       const uint8_t **poutbuf, int *poutbuf_size,
240
                       const uint8_t *buf, int buf_size)
241
1.37M
{
242
1.37M
    DiracParseContext *pc = s->priv_data;
243
1.37M
    int next;
244
245
1.37M
    *poutbuf      = NULL;
246
1.37M
    *poutbuf_size = 0;
247
248
1.37M
    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
249
852
        next          = buf_size;
250
852
        *poutbuf      = buf;
251
852
        *poutbuf_size = buf_size;
252
        /* Assume that data has been packetized into an encapsulation unit. */
253
1.37M
    } else {
254
1.37M
        next = find_frame_end(pc, buf, buf_size);
255
1.37M
        if (!pc->is_synced && next == -1)
256
            /* No frame start found yet. So throw away the entire buffer. */
257
18.8k
            return buf_size;
258
259
1.36M
        if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0)
260
866k
            return buf_size;
261
1.36M
    }
262
263
494k
    *poutbuf      = buf;
264
494k
    *poutbuf_size = buf_size;
265
494k
    return next;
266
1.37M
}
267
268
static void dirac_parse_close(AVCodecParserContext *s)
269
9.23k
{
270
9.23k
    DiracParseContext *pc = s->priv_data;
271
272
9.23k
    if (pc->buffer_size > 0)
273
2.70k
        av_freep(&pc->buffer);
274
9.23k
}
275
276
const AVCodecParser ff_dirac_parser = {
277
    .codec_ids      = { AV_CODEC_ID_DIRAC },
278
    .priv_data_size = sizeof(DiracParseContext),
279
    .parser_parse   = dirac_parse,
280
    .parser_close   = dirac_parse_close,
281
};