Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/mpeg4video_parser.c
Line
Count
Source
1
/*
2
 * MPEG-4 video frame extraction
3
 * Copyright (c) 2003 Fabrice Bellard
4
 * Copyright (c) 2003 Michael Niedermayer
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#define UNCHECKED_BITSTREAM_READER 1
24
25
#include "decode.h"
26
#include "parser.h"
27
#include "mpegvideo.h"
28
#include "mpeg4videodec.h"
29
#include "mpeg4videodefs.h"
30
#include "parser_internal.h"
31
32
struct Mp4vParseContext {
33
    ParseContext pc;
34
    Mpeg4DecContext dec_ctx;
35
    int first_picture;
36
};
37
38
/**
39
 * Find the end of the current frame in the bitstream.
40
 * @return the position of the first byte of the next frame, or -1
41
 */
42
static int mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
43
862k
{
44
862k
    int vop_found, i;
45
862k
    uint32_t state;
46
47
862k
    vop_found = pc->frame_start_found;
48
862k
    state     = pc->state;
49
50
862k
    i = 0;
51
862k
    if (!vop_found) {
52
61.9M
        for (i = 0; i < buf_size; i++) {
53
61.8M
            state = (state << 8) | buf[i];
54
61.8M
            if (state == VOP_STARTCODE) {
55
747k
                i++;
56
747k
                vop_found = 1;
57
747k
                break;
58
747k
            }
59
61.8M
        }
60
825k
    }
61
62
862k
    if (vop_found) {
63
        /* EOF considered as end of frame */
64
784k
        if (buf_size == 0)
65
1.65k
            return 0;
66
31.3M
        for (; i < buf_size; i++) {
67
31.3M
            state = (state << 8) | buf[i];
68
31.3M
            if ((state & 0xFFFFFF00) == 0x100) {
69
760k
                if (state == SLICE_STARTCODE || state == EXT_STARTCODE)
70
15.0k
                    continue;
71
745k
                pc->frame_start_found = 0;
72
745k
                pc->state             = -1;
73
745k
                return i - 3;
74
760k
            }
75
31.3M
        }
76
782k
    }
77
114k
    pc->frame_start_found = vop_found;
78
114k
    pc->state             = state;
79
114k
    return END_NOT_FOUND;
80
862k
}
81
82
/* XXX: make it use less memory */
83
static int mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx,
84
                               const uint8_t *buf, int buf_size)
85
755k
{
86
755k
    struct Mp4vParseContext *pc = s1->priv_data;
87
755k
    Mpeg4DecContext *dec_ctx = &pc->dec_ctx;
88
755k
    MPVContext *const s = &dec_ctx->h.c;
89
755k
    GetBitContext gb1, *gb = &gb1;
90
755k
    int ret;
91
92
755k
    s->avctx               = avctx;
93
94
755k
    if (avctx->extradata_size && pc->first_picture) {
95
6.70k
        init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
96
6.70k
        ret = ff_mpeg4_parse_picture_header(dec_ctx, gb, 1, 1);
97
6.70k
        if (ret < 0)
98
955
            av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata\n");
99
6.70k
    }
100
101
755k
    init_get_bits(gb, buf, 8 * buf_size);
102
755k
    ret = ff_mpeg4_parse_picture_header(dec_ctx, gb, 0, 1);
103
755k
    avctx->has_b_frames = !s->low_delay;
104
755k
    if (s->width && (!avctx->width || !avctx->height ||
105
636k
                     !avctx->coded_width || !avctx->coded_height)) {
106
22.4k
        ret = ff_set_dimensions(avctx, s->width, s->height);
107
22.4k
        if (ret < 0)
108
19.1k
            return ret;
109
22.4k
    }
110
736k
    if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->framerate.num>0 && ret>=0){
111
192k
        av_assert1(s1->pts == AV_NOPTS_VALUE);
112
192k
        av_assert1(s1->dts == AV_NOPTS_VALUE);
113
114
192k
        s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->framerate.num}, (AVRational){1, 1200000});
115
192k
    }
116
117
736k
    s1->pict_type     = s->pict_type;
118
736k
    pc->first_picture = 0;
119
736k
    return ret;
120
755k
}
121
122
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
123
5.14k
{
124
5.14k
    struct Mp4vParseContext *pc = s->priv_data;
125
126
5.14k
    pc->first_picture           = 1;
127
5.14k
    pc->dec_ctx.quant_precision       = 5;
128
5.14k
    pc->dec_ctx.h.c.slice_context_count = 1;
129
5.14k
    pc->dec_ctx.showed_packed_warning = 1;
130
5.14k
    return 0;
131
5.14k
}
132
133
static int mpeg4video_parse(AVCodecParserContext *s,
134
                            AVCodecContext *avctx,
135
                            const uint8_t **poutbuf, int *poutbuf_size,
136
                            const uint8_t *buf, int buf_size)
137
867k
{
138
867k
    ParseContext *pc = s->priv_data;
139
867k
    int next;
140
141
867k
    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
142
4.82k
        next = buf_size;
143
862k
    } else {
144
862k
        next = mpeg4_find_frame_end(pc, buf, buf_size);
145
146
862k
        if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
147
111k
            *poutbuf      = NULL;
148
111k
            *poutbuf_size = 0;
149
111k
            return buf_size;
150
111k
        }
151
862k
    }
152
755k
    mpeg4_decode_header(s, avctx, buf, buf_size);
153
154
755k
    *poutbuf      = buf;
155
755k
    *poutbuf_size = buf_size;
156
755k
    return next;
157
867k
}
158
159
const FFCodecParser ff_mpeg4video_parser = {
160
    PARSER_CODEC_LIST(AV_CODEC_ID_MPEG4),
161
    .priv_data_size = sizeof(struct Mp4vParseContext),
162
    .init           = mpeg4video_parse_init,
163
    .parse          = mpeg4video_parse,
164
    .close          = ff_parse_close,
165
};