mpeg4video_parser.c
Go to the documentation of this file.
1 /*
2  * MPEG4 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 "parser.h"
26 #include "mpegvideo.h"
27 #include "mpeg4video.h"
28 #include "mpeg4video_parser.h"
29 
34 };
35 
36 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
37  int vop_found, i;
38  uint32_t state;
39 
40  vop_found= pc->frame_start_found;
41  state= pc->state;
42 
43  i=0;
44  if(!vop_found){
45  for(i=0; i<buf_size; i++){
46  state= (state<<8) | buf[i];
47  if(state == 0x1B6){
48  i++;
49  vop_found=1;
50  break;
51  }
52  }
53  }
54 
55  if(vop_found){
56  /* EOF considered as end of frame */
57  if (buf_size == 0)
58  return 0;
59  for(; i<buf_size; i++){
60  state= (state<<8) | buf[i];
61  if((state&0xFFFFFF00) == 0x100){
62  pc->frame_start_found=0;
63  pc->state=-1;
64  return i-3;
65  }
66  }
67  }
68  pc->frame_start_found= vop_found;
69  pc->state= state;
70  return END_NOT_FOUND;
71 }
72 
73 /* XXX: make it use less memory */
76  const uint8_t *buf, int buf_size)
77 {
78  struct Mp4vParseContext *pc = s1->priv_data;
79  MpegEncContext *s = &pc->enc;
80  GetBitContext gb1, *gb = &gb1;
81  int ret;
82 
83  s->avctx = avctx;
85 
86  if (avctx->extradata_size && pc->first_picture){
87  init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
88  ret = ff_mpeg4_decode_picture_header(s, gb);
89  }
90 
91  init_get_bits(gb, buf, 8 * buf_size);
92  ret = ff_mpeg4_decode_picture_header(s, gb);
93  if (s->width && (!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height)) {
94  avcodec_set_dimensions(avctx, s->width, s->height);
95  }
96  if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->time_base.den>0 && ret>=0){
99 
100  s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->time_base.den}, (AVRational){1, 1200000});
101  }
102 
103  s1->pict_type= s->pict_type;
104  pc->first_picture = 0;
105  return ret;
106 }
107 
109 {
110  struct Mp4vParseContext *pc = s->priv_data;
111 
113 
114  pc->first_picture = 1;
115  pc->enc.quant_precision=5;
116  pc->enc.slice_context_count = 1;
117  return 0;
118 }
119 
121  AVCodecContext *avctx,
122  const uint8_t **poutbuf, int *poutbuf_size,
123  const uint8_t *buf, int buf_size)
124 {
125  ParseContext *pc = s->priv_data;
126  int next;
127 
129  next= buf_size;
130  }else{
131  next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
132 
133  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
134  *poutbuf = NULL;
135  *poutbuf_size = 0;
136  return buf_size;
137  }
138  }
139  av_mpeg4_decode_header(s, avctx, buf, buf_size);
140 
141  *poutbuf = buf;
142  *poutbuf_size = buf_size;
143  return next;
144 }
145 
146 
149  .priv_data_size = sizeof(struct Mp4vParseContext),
150  .parser_init = mpeg4video_parse_init,
151  .parser_parse = mpeg4video_parse,
152  .parser_close = ff_parse_close,
153  .split = ff_mpeg4video_split,
154 };
const char * s
Definition: avisynth_c.h:668
int coded_width
Bitstream width / height, may be different from width/height e.g.
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
mpegvideo header.
int frame_start_found
Definition: parser.h:34
int quant_precision
Definition: mpegvideo.h:578
int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: parser.c:288
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
int64_t time
time of current frame
Definition: mpegvideo.h:558
uint8_t
#define av_cold
Definition: attributes.h:78
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:343
static int mpeg4video_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:319
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:214
#define PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_USE_CODEC_TS
void ff_mpeg4videodec_static_init(void)
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:279
struct MpegEncContext enc
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
ret
Definition: avfilter.c:821
int width
picture width / height.
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:347
NULL
Definition: eval.c:55
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
main external API structure.
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:245
void * buf
Definition: avisynth_c.h:594
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
synthesis window for stochastic i
rational number numerator/denominator
Definition: rational.h:43
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
#define s1
Definition: regdef.h:38
#define END_NOT_FOUND
Definition: parser.h:40
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:377
static uint32_t state
Definition: trasher.c:27
MpegEncContext.
Definition: mpegvideo.h:241
struct AVCodecContext * avctx
Definition: mpegvideo.h:243
static int av_mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
int den
denominator
Definition: rational.h:45
int ff_mpeg4_decode_picture_header(MpegEncContext *s, GetBitContext *gb)
Decode mpeg4 headers.
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:190