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 
28 
29 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
30  int vop_found, i;
31  uint32_t state;
32 
33  vop_found= pc->frame_start_found;
34  state= pc->state;
35 
36  i=0;
37  if(!vop_found){
38  for(i=0; i<buf_size; i++){
39  state= (state<<8) | buf[i];
40  if(state == 0x1B6){
41  i++;
42  vop_found=1;
43  break;
44  }
45  }
46  }
47 
48  if(vop_found){
49  /* EOF considered as end of frame */
50  if (buf_size == 0)
51  return 0;
52  for(; i<buf_size; i++){
53  state= (state<<8) | buf[i];
54  if((state&0xFFFFFF00) == 0x100){
55  pc->frame_start_found=0;
56  pc->state=-1;
57  return i-3;
58  }
59  }
60  }
61  pc->frame_start_found= vop_found;
62  pc->state= state;
63  return END_NOT_FOUND;
64 }
65 
66 /* XXX: make it use less memory */
68  AVCodecContext *avctx,
69  const uint8_t *buf, int buf_size)
70 {
71  ParseContext1 *pc = s1->priv_data;
72  MpegEncContext *s = pc->enc;
73  GetBitContext gb1, *gb = &gb1;
74  int ret;
75 
76  s->avctx = avctx;
78 
79  if (avctx->extradata_size && pc->first_picture){
80  init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
81  ret = ff_mpeg4_decode_picture_header(s, gb);
82  }
83 
84  init_get_bits(gb, buf, 8 * buf_size);
85  ret = ff_mpeg4_decode_picture_header(s, gb);
86  if (s->width && (!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height)) {
87  avcodec_set_dimensions(avctx, s->width, s->height);
88  }
89  s1->pict_type= s->pict_type;
90  pc->first_picture = 0;
91  return ret;
92 }
93 
95 {
96  ParseContext1 *pc = s->priv_data;
97 
98  pc->enc = av_mallocz(sizeof(MpegEncContext));
99  if (!pc->enc)
100  return -1;
101  pc->first_picture = 1;
102  pc->enc->slice_context_count = 1;
103  return 0;
104 }
105 
107  AVCodecContext *avctx,
108  const uint8_t **poutbuf, int *poutbuf_size,
109  const uint8_t *buf, int buf_size)
110 {
111  ParseContext *pc = s->priv_data;
112  int next;
113 
115  next= buf_size;
116  }else{
117  next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
118 
119  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
120  *poutbuf = NULL;
121  *poutbuf_size = 0;
122  return buf_size;
123  }
124  }
125  av_mpeg4_decode_header(s, avctx, buf, buf_size);
126 
127  *poutbuf = buf;
128  *poutbuf_size = buf_size;
129  return next;
130 }
131 
132 
134  .codec_ids = { CODEC_ID_MPEG4 },
135  .priv_data_size = sizeof(ParseContext1),
136  .parser_init = mpeg4video_parse_init,
137  .parser_parse = mpeg4video_parse,
138  .parser_close = ff_parse1_close,
139  .split = ff_mpeg4video_split,
140 };
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:154
int coded_width
Bitstream width / height, may be different from width/height if lowres enabled.
Definition: avcodec.h:2543
struct ParseContext1 ParseContext1
int codec_ids[5]
Definition: avcodec.h:4542
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:304
void ff_parse1_close(AVCodecParserContext *s)
Definition: parser.c:294
#define av_cold
Definition: attributes.h:71
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:295
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:133
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:271
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:222
int width
picture width / height.
Definition: avcodec.h:1408
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:299
NULL
Definition: eval.c:50
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
main external API structure.
Definition: avcodec.h:1329
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:205
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
int extradata_size
Definition: avcodec.h:1388
int coded_height
Definition: avcodec.h:2543
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:354
#define s1
Definition: regdef.h:38
#define END_NOT_FOUND
Definition: parser.h:55
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:331
static uint32_t state
Definition: trasher.c:25
MpegEncContext.
Definition: mpegvideo.h:201
struct AVCodecContext * avctx
Definition: mpegvideo.h:203
static int av_mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:4447
int first_picture
Definition: parser.h:52
struct MpegEncContext * enc
Definition: parser.h:51
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.