File: apv_parser.c

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (213 lines) | stat: -rw-r--r-- 6,467 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/avassert.h"
#include "libavutil/buffer.h"
#include "libavutil/mem.h"

#include "avcodec.h"
#include "apv.h"
#include "cbs.h"
#include "cbs_apv.h"
#include "parser.h"
#include "parser_internal.h"

typedef struct APVParseContext {
    ParseContext pc;

    CodedBitstreamContext *cbc;
    CodedBitstreamFragment au;
} APVParseContext;

static const enum AVPixelFormat apv_format_table[5][5] = {
    { AV_PIX_FMT_GRAY8,    AV_PIX_FMT_GRAY10,     AV_PIX_FMT_GRAY12,     AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16 },
    { 0 }, // 4:2:0 is not valid.
    { AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV422P10,  AV_PIX_FMT_YUV422P12,  AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUV422P16 },
    { AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV444P10,  AV_PIX_FMT_YUV444P12,  AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUV444P16 },
    { AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUVA444P16 },
};

static int find_frame_end(APVParseContext *p, const uint8_t *buf, int buf_size)
{
    ParseContext *pc = &p->pc;
    int pic_found, i = 0;
    uint32_t state;

    pic_found = pc->frame_start_found;
    state = pc->state;

    if (buf_size == 0) {
        pc->frame_start_found = 0;
        pc->state = -1;
        return 0;
    }

    if (!pic_found) {
        for (; i < buf_size; i++) {
            state = (state << 8) | buf[i];
            if (state == APV_SIGNATURE) {
                i++;
                pic_found = 1;
                break;
            }
        }
    }

    if (pic_found) {
        for(; i < buf_size; i++) {
            state = (state << 8) | buf[i];
            if (state == APV_SIGNATURE) {
                pc->frame_start_found = 0;
                pc->state = -1;
                return i - 3;
            }
        }
    }

    pc->frame_start_found = pic_found;
    pc->state = state;
    return END_NOT_FOUND;
}

static void dummy_free(void *opaque, uint8_t *data)
{
    av_assert0(opaque == data);
}

static int parse(AVCodecParserContext *s,
                 AVCodecContext *avctx,
                 const uint8_t **poutbuf, int *poutbuf_size,
                 const uint8_t *buf, int buf_size)
{
    APVParseContext *p = s->priv_data;
    CodedBitstreamFragment *au = &p->au;
    AVBufferRef *ref = NULL;
    int next, ret;

    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
        next = buf_size;
    } else {
        next = find_frame_end(p, buf, buf_size);

        if (ff_combine_frame(&p->pc, next, &buf, &buf_size) < 0) {
            *poutbuf      = NULL;
            *poutbuf_size = 0;
            return buf_size;
        }
    }

    *poutbuf      = buf;
    *poutbuf_size = buf_size;

    if (!buf_size)
        return 0;

    ref = av_buffer_create((uint8_t *)buf, buf_size, dummy_free,
                           (void *)buf, AV_BUFFER_FLAG_READONLY);
    if (!ref)
        return next;

    p->cbc->log_ctx = avctx;

    ret = ff_cbs_read(p->cbc, au, ref, buf, buf_size);
    if (ret < 0) {
        av_log(avctx, AV_LOG_ERROR, "Failed to parse access unit.\n");
        goto end;
    }

    s->key_frame         = 1;
    s->pict_type         = AV_PICTURE_TYPE_I;
    s->field_order       = AV_FIELD_UNKNOWN;
    s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;

    for (int i = 0; i < au->nb_units; i++) {
        const CodedBitstreamUnit *pbu = &au->units[i];

        switch (pbu->type) {
        case APV_PBU_PRIMARY_FRAME: {
            const APVRawFrame *frame        = pbu->content;
            const APVRawFrameHeader *header = &frame->frame_header;
            const APVRawFrameInfo *info     = &header->frame_info;
            int bit_depth = info->bit_depth_minus8 + 8;

            if (bit_depth < 8 || bit_depth > 16 || bit_depth % 2)
                break;

            s->width                        = info->frame_width;
            s->height                       = info->frame_height;
            s->format                       = apv_format_table[info->chroma_format_idc][bit_depth - 4 >> 2];
            avctx->profile                  = info->profile_idc;
            avctx->level                    = info->level_idc;
            avctx->chroma_sample_location   = AVCHROMA_LOC_TOPLEFT;
            avctx->color_primaries          = header->color_primaries;
            avctx->color_trc                = header->transfer_characteristics;
            avctx->colorspace               = header->matrix_coefficients;
            avctx->color_range              = header->full_range_flag ? AVCOL_RANGE_JPEG
                                                                      : AVCOL_RANGE_MPEG;
            goto end;
        }
        default:
            break;
        }
    }

end:
    ff_cbs_fragment_reset(au);
    av_assert1(av_buffer_get_ref_count(ref) == 1);
    av_buffer_unref(&ref);
    p->cbc->log_ctx = NULL;

    return next;
}

static const CodedBitstreamUnitType decompose_unit_types[] = {
    APV_PBU_PRIMARY_FRAME,
};

static av_cold int init(AVCodecParserContext *s)
{
    APVParseContext *p = s->priv_data;
    int ret;

    ret = ff_cbs_init(&p->cbc, AV_CODEC_ID_APV, NULL);
    if (ret < 0)
        return ret;

    p->cbc->decompose_unit_types    = decompose_unit_types;
    p->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);

    return 0;
}

static av_cold void close(AVCodecParserContext *s)
{
    APVParseContext *p = s->priv_data;
    ParseContext *pc = &p->pc;

    av_freep(&pc->buffer);
    ff_cbs_fragment_free(&p->au);
    ff_cbs_close(&p->cbc);
}

const FFCodecParser ff_apv_parser = {
    PARSER_CODEC_LIST(AV_CODEC_ID_APV),
    .priv_data_size = sizeof(APVParseContext),
    .init           = init,
    .parse          = parse,
    .close          = close,
};