FFmpeg
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 
35 };
36 
37 /**
38  * Find the end of the current frame in the bitstream.
39  * @return the position of the first byte of the next frame, or -1
40  */
41 static int mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
42 {
43  int vop_found, i;
44  uint32_t state;
45 
46  vop_found = pc->frame_start_found;
47  state = pc->state;
48 
49  i = 0;
50  if (!vop_found) {
51  for (i = 0; i < buf_size; i++) {
52  state = (state << 8) | buf[i];
53  if (state == VOP_STARTCODE) {
54  i++;
55  vop_found = 1;
56  break;
57  }
58  }
59  }
60 
61  if (vop_found) {
62  /* EOF considered as end of frame */
63  if (buf_size == 0)
64  return 0;
65  for (; i < buf_size; i++) {
66  state = (state << 8) | buf[i];
67  if ((state & 0xFFFFFF00) == 0x100) {
69  continue;
70  pc->frame_start_found = 0;
71  pc->state = -1;
72  return i - 3;
73  }
74  }
75  }
76  pc->frame_start_found = vop_found;
77  pc->state = state;
78  return END_NOT_FOUND;
79 }
80 
81 /* XXX: make it use less memory */
83  const uint8_t *buf, int buf_size)
84 {
85  struct Mp4vParseContext *pc = s1->priv_data;
86  Mpeg4DecContext *dec_ctx = &pc->dec_ctx;
87  MpegEncContext *s = &dec_ctx->m;
88  GetBitContext gb1, *gb = &gb1;
89  int ret;
90 
91  s->avctx = avctx;
92 
93  if (avctx->extradata_size && pc->first_picture) {
94  init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
96  if (ret < 0)
97  av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata\n");
98  }
99 
100  init_get_bits(gb, buf, 8 * buf_size);
102  if (s->width && (!avctx->width || !avctx->height ||
103  !avctx->coded_width || !avctx->coded_height)) {
104  ret = ff_set_dimensions(avctx, s->width, s->height);
105  if (ret < 0)
106  return ret;
107  }
108  if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->framerate.num>0 && ret>=0){
109  av_assert1(s1->pts == AV_NOPTS_VALUE);
110  av_assert1(s1->dts == AV_NOPTS_VALUE);
111 
112  s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->framerate.num}, (AVRational){1, 1200000});
113  }
114 
115  s1->pict_type = s->pict_type;
116  pc->first_picture = 0;
117  return ret;
118 }
119 
121 {
122  struct Mp4vParseContext *pc = s->priv_data;
123 
124  pc->first_picture = 1;
125  pc->dec_ctx.quant_precision = 5;
126  pc->dec_ctx.m.slice_context_count = 1;
127  pc->dec_ctx.showed_packed_warning = 1;
128  return 0;
129 }
130 
132  AVCodecContext *avctx,
133  const uint8_t **poutbuf, int *poutbuf_size,
134  const uint8_t *buf, int buf_size)
135 {
136  ParseContext *pc = s->priv_data;
137  int next;
138 
139  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
140  next = buf_size;
141  } else {
142  next = mpeg4_find_frame_end(pc, buf, buf_size);
143 
144  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
145  *poutbuf = NULL;
146  *poutbuf_size = 0;
147  return buf_size;
148  }
149  }
150  mpeg4_decode_header(s, avctx, buf, buf_size);
151 
152  *poutbuf = buf;
153  *poutbuf_size = buf_size;
154  return next;
155 }
156 
159  .priv_data_size = sizeof(struct Mp4vParseContext),
160  .parser_init = mpeg4video_parse_init,
161  .parser_parse = mpeg4video_parse,
162  .parser_close = ff_parse_close,
163 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AVCodecParserContext::pts
int64_t pts
Definition: avcodec.h:2783
dec_ctx
static AVCodecContext * dec_ctx
Definition: decode_filter_audio.c:47
AVCodecParserContext::pict_type
int pict_type
Definition: avcodec.h:2772
mpeg4videodec.h
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:290
mpeg4_find_frame_end
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.
Definition: mpeg4video_parser.c:41
mpegvideo.h
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:94
SLICE_STARTCODE
#define SLICE_STARTCODE
Definition: mpeg4videodefs.h:60
ParseContext::state
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
mpeg4video_parse
static int mpeg4video_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: mpeg4video_parser.c:131
ParseContext
Definition: parser.h:28
VOP_STARTCODE
#define VOP_STARTCODE
Definition: mpeg4videodefs.h:59
GetBitContext
Definition: get_bits.h:108
Mp4vParseContext::first_picture
int first_picture
Definition: mpeg4video_parser.c:34
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:647
AVCodecParserContext::dts
int64_t dts
Definition: avcodec.h:2784
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:538
s
#define s(width, name)
Definition: cbs_vp9.c:198
decode.h
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
mpeg4_decode_header
static int mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: mpeg4video_parser.c:82
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
state
static struct @466 state
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
AVCodecParserContext::flags
int flags
Definition: avcodec.h:2797
Mpeg4DecContext
Definition: mpeg4videodec.h:34
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2924
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
EXT_STARTCODE
#define EXT_STARTCODE
Definition: mpeg4videodefs.h:61
ff_combine_frame
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:203
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2798
mpeg4video_parse_init
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
Definition: mpeg4video_parser.c:120
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:537
ff_mpeg4_decode_picture_header
int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb, int header, int parse_only)
Decode MPEG-4 headers.
Definition: mpeg4videodec.c:3466
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
parser.h
AVCodecContext::height
int height
Definition: avcodec.h:632
Mp4vParseContext
Definition: mpeg4video_parser.c:31
Mp4vParseContext::dec_ctx
Mpeg4DecContext dec_ctx
Definition: mpeg4video_parser.c:33
AVCodecParserContext
Definition: avcodec.h:2764
mpeg4videodefs.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:451
ff_mpeg4video_parser
const AVCodecParser ff_mpeg4video_parser
Definition: mpeg4video_parser.c:157
Mp4vParseContext::pc
ParseContext pc
Definition: mpeg4video_parser.c:32
PARSER_FLAG_USE_CODEC_TS
#define PARSER_FLAG_USE_CODEC_TS
Definition: avcodec.h:2802
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:647
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParser
Definition: avcodec.h:2923
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:632
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVCodecParserContext::priv_data
void * priv_data
Definition: avcodec.h:2765
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:73