FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include "mpeg4video_parser.h"
30 
35 };
36 
37 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
38 {
39  int vop_found, i;
40  uint32_t state;
41 
42  vop_found = pc->frame_start_found;
43  state = pc->state;
44 
45  i = 0;
46  if (!vop_found) {
47  for (i = 0; i < buf_size; i++) {
48  state = (state << 8) | buf[i];
49  if (state == 0x1B6) {
50  i++;
51  vop_found = 1;
52  break;
53  }
54  }
55  }
56 
57  if (vop_found) {
58  /* EOF considered as end of frame */
59  if (buf_size == 0)
60  return 0;
61  for (; i < buf_size; i++) {
62  state = (state << 8) | buf[i];
63  if ((state & 0xFFFFFF00) == 0x100) {
64  pc->frame_start_found = 0;
65  pc->state = -1;
66  return i - 3;
67  }
68  }
69  }
70  pc->frame_start_found = vop_found;
71  pc->state = state;
72  return END_NOT_FOUND;
73 }
74 
75 /* XXX: make it use less memory */
77  const uint8_t *buf, int buf_size)
78 {
79  struct Mp4vParseContext *pc = s1->priv_data;
81  MpegEncContext *s = &dec_ctx->m;
82  GetBitContext gb1, *gb = &gb1;
83  int ret;
84 
85  s->avctx = avctx;
87 
88  if (avctx->extradata_size && pc->first_picture) {
89  init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
90  ret = ff_mpeg4_decode_picture_header(dec_ctx, gb);
91  if (ret < -1)
92  av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata\n");
93  }
94 
95  init_get_bits(gb, buf, 8 * buf_size);
96  ret = ff_mpeg4_decode_picture_header(dec_ctx, gb);
97  if (s->width && (!avctx->width || !avctx->height ||
98  !avctx->coded_width || !avctx->coded_height)) {
99  ret = ff_set_dimensions(avctx, s->width, s->height);
100  if (ret < 0)
101  return ret;
102  }
103  if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->time_base.den>0 && ret>=0){
104  av_assert1(s1->pts == AV_NOPTS_VALUE);
105  av_assert1(s1->dts == AV_NOPTS_VALUE);
106 
107  s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->time_base.den}, (AVRational){1, 1200000});
108  }
109 
110  s1->pict_type = s->pict_type;
111  pc->first_picture = 0;
112  return ret;
113 }
114 
116 {
117  struct Mp4vParseContext *pc = s->priv_data;
118 
120 
121  pc->first_picture = 1;
122  pc->dec_ctx.m.quant_precision = 5;
123  pc->dec_ctx.m.slice_context_count = 1;
125  return 0;
126 }
127 
129  AVCodecContext *avctx,
130  const uint8_t **poutbuf, int *poutbuf_size,
131  const uint8_t *buf, int buf_size)
132 {
133  ParseContext *pc = s->priv_data;
134  int next;
135 
137  next = buf_size;
138  } else {
139  next = ff_mpeg4_find_frame_end(pc, buf, buf_size);
140 
141  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
142  *poutbuf = NULL;
143  *poutbuf_size = 0;
144  return buf_size;
145  }
146  }
147  mpeg4_decode_header(s, avctx, buf, buf_size);
148 
149  *poutbuf = buf;
150  *poutbuf_size = buf_size;
151  return next;
152 }
153 
156  .priv_data_size = sizeof(struct Mp4vParseContext),
157  .parser_init = mpeg4video_parse_init,
158  .parser_parse = mpeg4video_parse,
159  .parser_close = ff_parse_close,
160  .split = ff_mpeg4video_split,
161 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1878
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
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:210
int codec_ids[5]
Definition: avcodec.h:5149
mpegvideo header.
int frame_start_found
Definition: parser.h:34
int quant_precision
Definition: mpegvideo.h:398
int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: parser.c:333
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1813
int64_t time
time of current frame
Definition: mpegvideo.h:388
uint8_t
#define av_cold
Definition: attributes.h:82
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1791
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:177
#define av_log(a,...)
static int mpeg4video_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
MpegEncContext m
Definition: mpeg4video.h:66
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
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:153
AVCodecParser ff_mpeg4video_parser
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:250
void ff_mpeg4videodec_static_init(void)
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:326
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
static int mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
int width
picture width / height.
Definition: avcodec.h:1863
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:181
int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode MPEG-4 headers.
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
main external API structure.
Definition: avcodec.h:1676
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:97
static struct @246 state
void * buf
Definition: avisynth_c.h:690
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
int extradata_size
Definition: avcodec.h:1792
int showed_packed_warning
flag for having shown the warning about invalid Divx B-frames
Definition: mpeg4video.h:101
int coded_height
Definition: avcodec.h:1878
Rational number (pair of numerator and denominator).
Definition: rational.h:58
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:406
#define s1
Definition: regdef.h:38
#define END_NOT_FOUND
Definition: parser.h:40
MpegEncContext.
Definition: mpegvideo.h:78
struct AVCodecContext * avctx
Definition: mpegvideo.h:95
common internal api header.
static AVCodecContext * dec_ctx
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:5015
int den
Denominator.
Definition: rational.h:60
Mpeg4DecContext dec_ctx
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.
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:242
#define PARSER_FLAG_USE_CODEC_TS
Definition: avcodec.h:5019