Coverage Report

Created: 2026-09-14 08:00

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
674k
{
44
674k
    int vop_found, i;
45
674k
    uint32_t state;
46
47
674k
    vop_found = pc->frame_start_found;
48
674k
    state     = pc->state;
49
50
674k
    i = 0;
51
674k
    if (!vop_found) {
52
51.9M
        for (i = 0; i < buf_size; i++) {
53
51.8M
            state = (state << 8) | buf[i];
54
51.8M
            if (state == VOP_STARTCODE) {
55
570k
                i++;
56
570k
                vop_found = 1;
57
570k
                break;
58
570k
            }
59
51.8M
        }
60
632k
    }
61
62
674k
    if (vop_found) {
63
        /* EOF considered as end of frame */
64
612k
        if (buf_size == 0)
65
1.49k
            return 0;
66
22.5M
        for (; i < buf_size; i++) {
67
22.4M
            state = (state << 8) | buf[i];
68
22.4M
            if ((state & 0xFFFFFF00) == 0x100) {
69
579k
                if (state == SLICE_STARTCODE || state == EXT_STARTCODE)
70
11.2k
                    continue;
71
568k
                pc->frame_start_found = 0;
72
568k
                pc->state             = -1;
73
568k
                return i - 3;
74
579k
            }
75
22.4M
        }
76
610k
    }
77
104k
    pc->frame_start_found = vop_found;
78
104k
    pc->state             = state;
79
104k
    return END_NOT_FOUND;
80
674k
}
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
577k
{
86
577k
    struct Mp4vParseContext *pc = s1->priv_data;
87
577k
    Mpeg4DecContext *dec_ctx = &pc->dec_ctx;
88
577k
    MPVContext *const s = &dec_ctx->h.c;
89
577k
    GetBitContext gb1, *gb = &gb1;
90
577k
    int ret;
91
92
577k
    s->avctx               = avctx;
93
94
577k
    if (avctx->extradata_size && pc->first_picture) {
95
7.83k
        init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
96
7.83k
        ret = ff_mpeg4_parse_picture_header(dec_ctx, gb, 1, 1);
97
7.83k
        if (ret < 0)
98
1.26k
            av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata\n");
99
7.83k
    }
100
101
577k
    init_get_bits(gb, buf, 8 * buf_size);
102
577k
    ret = ff_mpeg4_parse_picture_header(dec_ctx, gb, 0, 1);
103
577k
    avctx->has_b_frames = !s->low_delay;
104
577k
    if (s->width && (!avctx->width || !avctx->height ||
105
478k
                     !avctx->coded_width || !avctx->coded_height)) {
106
19.5k
        ret = ff_set_dimensions(avctx, s->width, s->height);
107
19.5k
        if (ret < 0)
108
16.9k
            return ret;
109
19.5k
    }
110
560k
    if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->framerate.num>0 && ret>=0){
111
156k
        av_assert1(s1->pts == AV_NOPTS_VALUE);
112
156k
        av_assert1(s1->dts == AV_NOPTS_VALUE);
113
114
156k
        s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->framerate.num}, (AVRational){1, 1200000});
115
156k
    }
116
117
560k
    s1->pict_type     = s->pict_type;
118
560k
    pc->first_picture = 0;
119
560k
    return ret;
120
577k
}
121
122
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
123
4.24k
{
124
4.24k
    struct Mp4vParseContext *pc = s->priv_data;
125
126
4.24k
    pc->first_picture           = 1;
127
4.24k
    pc->dec_ctx.quant_precision       = 5;
128
4.24k
    pc->dec_ctx.h.c.slice_context_count = 1;
129
4.24k
    pc->dec_ctx.showed_packed_warning = 1;
130
4.24k
    return 0;
131
4.24k
}
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
678k
{
138
678k
    ParseContext *pc = s->priv_data;
139
678k
    int next;
140
141
678k
    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
142
4.44k
        next = buf_size;
143
674k
    } else {
144
674k
        next = mpeg4_find_frame_end(pc, buf, buf_size);
145
146
674k
        if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
147
101k
            *poutbuf      = NULL;
148
101k
            *poutbuf_size = 0;
149
101k
            return buf_size;
150
101k
        }
151
674k
    }
152
577k
    mpeg4_decode_header(s, avctx, buf, buf_size);
153
154
577k
    *poutbuf      = buf;
155
577k
    *poutbuf_size = buf_size;
156
577k
    return next;
157
678k
}
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
};