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 "internal.h"
26 #include "parser.h"
27 #include "mpegvideo.h"
28 #include "mpeg4video.h"
29 #if FF_API_FLAG_TRUNCATED
30 /* Nuke this header when removing FF_API_FLAG_TRUNCATED */
31 #include "mpeg4video_parser.h"
32 #endif
33 
38 };
39 
40 #if FF_API_FLAG_TRUNCATED
41 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
42 #else
43 /**
44  * Find the end of the current frame in the bitstream.
45  * @return the position of the first byte of the next frame, or -1
46  */
47 static int mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
48 #endif
49 {
50  int vop_found, i;
51  uint32_t state;
52 
53  vop_found = pc->frame_start_found;
54  state = pc->state;
55 
56  i = 0;
57  if (!vop_found) {
58  for (i = 0; i < buf_size; i++) {
59  state = (state << 8) | buf[i];
60  if (state == VOP_STARTCODE) {
61  i++;
62  vop_found = 1;
63  break;
64  }
65  }
66  }
67 
68  if (vop_found) {
69  /* EOF considered as end of frame */
70  if (buf_size == 0)
71  return 0;
72  for (; i < buf_size; i++) {
73  state = (state << 8) | buf[i];
74  if ((state & 0xFFFFFF00) == 0x100) {
76  continue;
77  pc->frame_start_found = 0;
78  pc->state = -1;
79  return i - 3;
80  }
81  }
82  }
83  pc->frame_start_found = vop_found;
84  pc->state = state;
85  return END_NOT_FOUND;
86 }
87 
88 /* XXX: make it use less memory */
90  const uint8_t *buf, int buf_size)
91 {
92  struct Mp4vParseContext *pc = s1->priv_data;
93  Mpeg4DecContext *dec_ctx = &pc->dec_ctx;
94  MpegEncContext *s = &dec_ctx->m;
95  GetBitContext gb1, *gb = &gb1;
96  int ret;
97 
98  s->avctx = avctx;
99  s->current_picture_ptr = &s->current_picture;
100 
101  if (avctx->extradata_size && pc->first_picture) {
102  init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
104  if (ret < 0)
105  av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata\n");
106  }
107 
108  init_get_bits(gb, buf, 8 * buf_size);
110  if (s->width && (!avctx->width || !avctx->height ||
111  !avctx->coded_width || !avctx->coded_height)) {
112  ret = ff_set_dimensions(avctx, s->width, s->height);
113  if (ret < 0)
114  return ret;
115  }
116  if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->time_base.den>0 && ret>=0){
117  av_assert1(s1->pts == AV_NOPTS_VALUE);
118  av_assert1(s1->dts == AV_NOPTS_VALUE);
119 
120  s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->time_base.den}, (AVRational){1, 1200000});
121  }
122 
123  s1->pict_type = s->pict_type;
124  pc->first_picture = 0;
125  return ret;
126 }
127 
129 {
130  struct Mp4vParseContext *pc = s->priv_data;
131 
133 
134  pc->first_picture = 1;
135  pc->dec_ctx.m.quant_precision = 5;
136  pc->dec_ctx.m.slice_context_count = 1;
137  pc->dec_ctx.showed_packed_warning = 1;
138  return 0;
139 }
140 
142  AVCodecContext *avctx,
143  const uint8_t **poutbuf, int *poutbuf_size,
144  const uint8_t *buf, int buf_size)
145 {
146  ParseContext *pc = s->priv_data;
147  int next;
148 
149  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
150  next = buf_size;
151  } else {
152 #if FF_API_FLAG_TRUNCATED
153  next = ff_mpeg4_find_frame_end(pc, buf, buf_size);
154 #else
155  next = mpeg4_find_frame_end(pc, buf, buf_size);
156 #endif
157 
158  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
159  *poutbuf = NULL;
160  *poutbuf_size = 0;
161  return buf_size;
162  }
163  }
164  mpeg4_decode_header(s, avctx, buf, buf_size);
165 
166  *poutbuf = buf;
167  *poutbuf_size = buf_size;
168  return next;
169 }
170 
173  .priv_data_size = sizeof(struct Mp4vParseContext),
174  .parser_init = mpeg4video_parse_init,
175  .parser_parse = mpeg4video_parse,
176  .parser_close = ff_parse_close,
177 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:62
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:286
internal.h
mpegvideo.h
ff_mpeg4videodec_static_init
void ff_mpeg4videodec_static_init(void)
Definition: mpeg4videodec.c:3377
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:660
state
static struct @319 state
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:141
ParseContext
Definition: parser.h:28
GetBitContext
Definition: get_bits.h:62
Mp4vParseContext::first_picture
int first_picture
Definition: mpeg4video_parser.c:37
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:571
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:485
s
#define s(width, name)
Definition: cbs_vp9.c:257
s1
#define s1
Definition: regdef.h:38
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:141
mpeg4_decode_header
static int mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: mpeg4video_parser.c:89
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
Mpeg4DecContext
Definition: mpeg4video.h:74
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2935
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
ff_mpeg4_decode_picture_header
int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb, int header)
Decode MPEG-4 headers.
Definition: mpeg4videodec.c:3221
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:201
EXT_STARTCODE
#define EXT_STARTCODE
Definition: mpeg4video.h:67
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2809
mpeg4video_parse_init
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
Definition: mpeg4video_parser.c:128
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:484
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
parser.h
AVCodecContext::height
int height
Definition: avcodec.h:556
Mp4vParseContext
Definition: mpeg4video_parser.c:34
Mp4vParseContext::dec_ctx
Mpeg4DecContext dec_ctx
Definition: mpeg4video_parser.c:36
AVCodecParserContext
Definition: avcodec.h:2775
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:383
ff_mpeg4video_parser
const AVCodecParser ff_mpeg4video_parser
Definition: mpeg4video_parser.c:171
mpeg4video.h
Mp4vParseContext::pc
ParseContext pc
Definition: mpeg4video_parser.c:35
PARSER_FLAG_USE_CODEC_TS
#define PARSER_FLAG_USE_CODEC_TS
Definition: avcodec.h:2813
VOP_STARTCODE
#define VOP_STARTCODE
Definition: mpeg4video.h:65
ff_mpeg4_find_frame_end
int ff_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
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:571
mpeg4video_parser.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:86
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParser
Definition: avcodec.h:2934
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:71
SLICE_STARTCODE
#define SLICE_STARTCODE
Definition: mpeg4video.h:66
dec_ctx
static AVCodecContext * dec_ctx
Definition: filtering_audio.c:44