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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "parser.h"
24 #include "mpegvideo.h"
25 #include "mpeg4video.h"
26 #include "mpeg4video_parser.h"
27 
32 };
33 
34 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
35  int vop_found, i;
36  uint32_t state;
37 
38  vop_found= pc->frame_start_found;
39  state= pc->state;
40 
41  i=0;
42  if(!vop_found){
43  for(i=0; i<buf_size; i++){
44  state= (state<<8) | buf[i];
45  if(state == 0x1B6){
46  i++;
47  vop_found=1;
48  break;
49  }
50  }
51  }
52 
53  if(vop_found){
54  /* EOF considered as end of frame */
55  if (buf_size == 0)
56  return 0;
57  for(; i<buf_size; i++){
58  state= (state<<8) | buf[i];
59  if((state&0xFFFFFF00) == 0x100){
60  pc->frame_start_found=0;
61  pc->state=-1;
62  return i-3;
63  }
64  }
65  }
66  pc->frame_start_found= vop_found;
67  pc->state= state;
68  return END_NOT_FOUND;
69 }
70 
71 /* XXX: make it use less memory */
74  const uint8_t *buf, int buf_size)
75 {
76  struct Mp4vParseContext *pc = s1->priv_data;
77  MpegEncContext *s = &pc->enc;
78  GetBitContext gb1, *gb = &gb1;
79  int ret;
80 
81  s->avctx = avctx;
83 
84  if (avctx->extradata_size && pc->first_picture){
85  init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
86  ret = ff_mpeg4_decode_picture_header(s, gb);
87  }
88 
89  init_get_bits(gb, buf, 8 * buf_size);
90  ret = ff_mpeg4_decode_picture_header(s, gb);
91  if (s->width && (!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height)) {
92  avcodec_set_dimensions(avctx, s->width, s->height);
93  }
94  s1->pict_type= s->pict_type;
95  pc->first_picture = 0;
96  return ret;
97 }
98 
100 {
101  struct Mp4vParseContext *pc = s->priv_data;
102 
104 
105  pc->first_picture = 1;
106  pc->enc.slice_context_count = 1;
107  return 0;
108 }
109 
111  AVCodecContext *avctx,
112  const uint8_t **poutbuf, int *poutbuf_size,
113  const uint8_t *buf, int buf_size)
114 {
115  ParseContext *pc = s->priv_data;
116  int next;
117 
119  next= buf_size;
120  }else{
121  next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
122 
123  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
124  *poutbuf = NULL;
125  *poutbuf_size = 0;
126  return buf_size;
127  }
128  }
129  av_mpeg4_decode_header(s, avctx, buf, buf_size);
130 
131  *poutbuf = buf;
132  *poutbuf_size = buf_size;
133  return next;
134 }
135 
136 
139  .priv_data_size = sizeof(struct Mp4vParseContext),
140  .parser_init = mpeg4video_parse_init,
141  .parser_parse = mpeg4video_parse,
142  .parser_close = ff_parse_close,
143  .split = ff_mpeg4video_split,
144 };
void ff_mpeg4_init_tables(void)
Definition: mpeg4videodec.c:48
int coded_width
Bitstream width / height, may be different from width/height.
Definition: avcodec.h:1515
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int codec_ids[5]
Definition: avcodec.h:3855
mpegvideo header.
int frame_start_found
Definition: parser.h:34
int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: parser.c:289
uint8_t
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:314
static int mpeg4video_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:290
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:215
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:280
struct MpegEncContext enc
int width
picture width / height.
Definition: avcodec.h:1508
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:318
NULL
Definition: eval.c:52
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
main external API structure.
Definition: avcodec.h:1339
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:216
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
int extradata_size
Definition: avcodec.h:1455
int coded_height
Definition: avcodec.h:1515
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
#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:350
static uint32_t state
Definition: trasher.c:27
MpegEncContext.
Definition: mpegvideo.h:212
struct AVCodecContext * avctx
Definition: mpegvideo.h:214
static int av_mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
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.