FFmpeg
Loading...
Searching...
No Matches
mpeg4video_parser.c
Go to the documentation of this file.
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
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 */
42static int mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
43{
44 int vop_found, i;
45 uint32_t state;
46
47 vop_found = pc->frame_start_found;
48 state = pc->state;
49
50 i = 0;
51 if (!vop_found) {
52 for (i = 0; i < buf_size; i++) {
53 state = (state << 8) | buf[i];
54 if (state == VOP_STARTCODE) {
55 i++;
56 vop_found = 1;
57 break;
58 }
59 }
60 }
61
62 if (vop_found) {
63 /* EOF considered as end of frame */
64 if (buf_size == 0)
65 return 0;
66 for (; i < buf_size; i++) {
67 state = (state << 8) | buf[i];
68 if ((state & 0xFFFFFF00) == 0x100) {
70 continue;
71 pc->frame_start_found = 0;
72 pc->state = -1;
73 return i - 3;
74 }
75 }
76 }
77 pc->frame_start_found = vop_found;
78 pc->state = state;
79 return END_NOT_FOUND;
80}
81
82/* XXX: make it use less memory */
84 const uint8_t *buf, int buf_size)
85{
86 struct Mp4vParseContext *pc = s1->priv_data;
87 Mpeg4DecContext *dec_ctx = &pc->dec_ctx;
88 MPVContext *const s = &dec_ctx->h.c;
89 GetBitContext gb1, *gb = &gb1;
90 int ret;
91
92 s->avctx = avctx;
93
94 if (avctx->extradata_size && pc->first_picture) {
95 init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
97 if (ret < 0)
98 av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata\n");
99 }
100
101 init_get_bits(gb, buf, 8 * buf_size);
102 ret = ff_mpeg4_parse_picture_header(dec_ctx, gb, 0, 1);
103 avctx->has_b_frames = !s->low_delay;
104 if (s->width && (!avctx->width || !avctx->height ||
105 !avctx->coded_width || !avctx->coded_height)) {
106 ret = ff_set_dimensions(avctx, s->width, s->height);
107 if (ret < 0)
108 return ret;
109 }
110 if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->framerate.num>0 && ret>=0){
113
114 s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->framerate.num}, (AVRational){1, 1200000});
115 }
116
117 s1->pict_type = s->pict_type;
118 pc->first_picture = 0;
119 return ret;
120}
121
123{
124 struct Mp4vParseContext *pc = s->priv_data;
125
126 pc->first_picture = 1;
127 pc->dec_ctx.quant_precision = 5;
128 pc->dec_ctx.h.c.slice_context_count = 1;
129 pc->dec_ctx.showed_packed_warning = 1;
130 return 0;
131}
132
134 AVCodecContext *avctx,
135 const uint8_t **poutbuf, int *poutbuf_size,
136 const uint8_t *buf, int buf_size)
137{
138 ParseContext *pc = s->priv_data;
139 int next;
140
141 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
142 next = buf_size;
143 } else {
144 next = mpeg4_find_frame_end(pc, buf, buf_size);
145
146 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
147 *poutbuf = NULL;
148 *poutbuf_size = 0;
149 return buf_size;
150 }
151 }
152 mpeg4_decode_header(s, avctx, buf, buf_size);
153
154 *poutbuf = buf;
155 *poutbuf_size = buf_size;
156 return next;
157}
158
161 .priv_data_size = sizeof(struct Mp4vParseContext),
163 .parse = mpeg4video_parse,
164 .close = ff_parse_close,
165};
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define PARSER_FLAG_USE_CODEC_TS
Definition avcodec.h:2655
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
static AVCodecContext * dec_ctx
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static struct @346255127015250356166251341105367306144006377143 state
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define av_cold
Definition attributes.h:117
static int mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
static int mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
static int mpeg4video_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
const FFCodecParser ff_mpeg4video_parser
int ff_mpeg4_parse_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb, int header, int parse_only)
Decode MPEG-4 headers.
#define EXT_STARTCODE
#define SLICE_STARTCODE
#define VOP_STARTCODE
mpegvideo header.
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition parser.c:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
main external API structure.
Definition avcodec.h:443
int width
picture width / height.
Definition avcodec.h:604
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition avcodec.h:709
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition avcodec.h:619
Rational number (pair of numerator and denominator).
Definition rational.h:58
Mpeg4DecContext dec_ctx
uint32_t state
contains the last few bytes in MSB order
Definition parser.h:33
int frame_start_found
Definition parser.h:34
#define av_log(a,...)