Coverage Report

Created: 2026-07-25 07:52

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
681k
{
44
681k
    int vop_found, i;
45
681k
    uint32_t state;
46
47
681k
    vop_found = pc->frame_start_found;
48
681k
    state     = pc->state;
49
50
681k
    i = 0;
51
681k
    if (!vop_found) {
52
55.9M
        for (i = 0; i < buf_size; i++) {
53
55.8M
            state = (state << 8) | buf[i];
54
55.8M
            if (state == VOP_STARTCODE) {
55
579k
                i++;
56
579k
                vop_found = 1;
57
579k
                break;
58
579k
            }
59
55.8M
        }
60
637k
    }
61
62
681k
    if (vop_found) {
63
        /* EOF considered as end of frame */
64
623k
        if (buf_size == 0)
65
1.48k
            return 0;
66
23.7M
        for (; i < buf_size; i++) {
67
23.6M
            state = (state << 8) | buf[i];
68
23.6M
            if ((state & 0xFFFFFF00) == 0x100) {
69
591k
                if (state == SLICE_STARTCODE || state == EXT_STARTCODE)
70
13.4k
                    continue;
71
577k
                pc->frame_start_found = 0;
72
577k
                pc->state             = -1;
73
577k
                return i - 3;
74
591k
            }
75
23.6M
        }
76
622k
    }
77
101k
    pc->frame_start_found = vop_found;
78
101k
    pc->state             = state;
79
101k
    return END_NOT_FOUND;
80
681k
}
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
586k
{
86
586k
    struct Mp4vParseContext *pc = s1->priv_data;
87
586k
    Mpeg4DecContext *dec_ctx = &pc->dec_ctx;
88
586k
    MPVContext *const s = &dec_ctx->h.c;
89
586k
    GetBitContext gb1, *gb = &gb1;
90
586k
    int ret;
91
92
586k
    s->avctx               = avctx;
93
94
586k
    if (avctx->extradata_size && pc->first_picture) {
95
7.21k
        init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
96
7.21k
        ret = ff_mpeg4_parse_picture_header(dec_ctx, gb, 1, 1);
97
7.21k
        if (ret < 0)
98
1.22k
            av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata\n");
99
7.21k
    }
100
101
586k
    init_get_bits(gb, buf, 8 * buf_size);
102
586k
    ret = ff_mpeg4_parse_picture_header(dec_ctx, gb, 0, 1);
103
586k
    avctx->has_b_frames = !s->low_delay;
104
586k
    if (s->width && (!avctx->width || !avctx->height ||
105
488k
                     !avctx->coded_width || !avctx->coded_height)) {
106
19.1k
        ret = ff_set_dimensions(avctx, s->width, s->height);
107
19.1k
        if (ret < 0)
108
16.4k
            return ret;
109
19.1k
    }
110
570k
    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
570k
    s1->pict_type     = s->pict_type;
118
570k
    pc->first_picture = 0;
119
570k
    return ret;
120
586k
}
121
122
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
123
4.32k
{
124
4.32k
    struct Mp4vParseContext *pc = s->priv_data;
125
126
4.32k
    pc->first_picture           = 1;
127
4.32k
    pc->dec_ctx.quant_precision       = 5;
128
4.32k
    pc->dec_ctx.h.c.slice_context_count = 1;
129
4.32k
    pc->dec_ctx.showed_packed_warning = 1;
130
4.32k
    return 0;
131
4.32k
}
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
685k
{
138
685k
    ParseContext *pc = s->priv_data;
139
685k
    int next;
140
141
685k
    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
142
4.37k
        next = buf_size;
143
681k
    } else {
144
681k
        next = mpeg4_find_frame_end(pc, buf, buf_size);
145
146
681k
        if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
147
98.9k
            *poutbuf      = NULL;
148
98.9k
            *poutbuf_size = 0;
149
98.9k
            return buf_size;
150
98.9k
        }
151
681k
    }
152
586k
    mpeg4_decode_header(s, avctx, buf, buf_size);
153
154
586k
    *poutbuf      = buf;
155
586k
    *poutbuf_size = buf_size;
156
586k
    return next;
157
685k
}
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
};