Libav
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 "internal.h"
24 #include "parser.h"
25 #include "mpegvideo.h"
26 #include "mpeg4video.h"
27 #include "mpeg4video_parser.h"
28 
33 };
34 
35 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
36 {
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 */
75  const uint8_t *buf, int buf_size)
76 {
77  struct Mp4vParseContext *pc = s1->priv_data;
79  MpegEncContext *s = &dec_ctx->m;
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(dec_ctx, gb);
89  }
90 
91  init_get_bits(gb, buf, 8 * buf_size);
92  ret = ff_mpeg4_decode_picture_header(dec_ctx, gb);
93  if (s->width && (!avctx->width || !avctx->height ||
94  !avctx->coded_width || !avctx->coded_height)) {
95  ret = ff_set_dimensions(avctx, s->width, s->height);
96  if (ret < 0)
97  return ret;
98  }
99  s1->pict_type = s->pict_type;
100  pc->first_picture = 0;
101  return ret;
102 }
103 
105 {
106  struct Mp4vParseContext *pc = s->priv_data;
107 
108  pc->first_picture = 1;
109  pc->dec_ctx.m.slice_context_count = 1;
110  return 0;
111 }
112 
114  AVCodecContext *avctx,
115  const uint8_t **poutbuf, int *poutbuf_size,
116  const uint8_t *buf, int buf_size)
117 {
118  ParseContext *pc = s->priv_data;
119  int next;
120 
122  next = buf_size;
123  } else {
124  next = ff_mpeg4_find_frame_end(pc, buf, buf_size);
125 
126  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
127  *poutbuf = NULL;
128  *poutbuf_size = 0;
129  return buf_size;
130  }
131  }
132  mpeg4_decode_header(s, avctx, buf, buf_size);
133 
134  *poutbuf = buf;
135  *poutbuf_size = buf_size;
136  return next;
137 }
138 
141  .priv_data_size = sizeof(struct Mp4vParseContext),
142  .parser_init = mpeg4video_parse_init,
143  .parser_parse = mpeg4video_parse,
144  .parser_close = ff_parse_close,
145  .split = ff_mpeg4video_split,
146 };
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1228
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:133
int codec_ids[5]
Definition: avcodec.h:3816
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:294
uint8_t
#define av_cold
Definition: attributes.h:66
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1158
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:306
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:63
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:282
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:217
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:287
static int mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
int width
picture width / height.
Definition: avcodec.h:1218
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:310
int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode mpeg4 headers.
NULL
Definition: eval.c:55
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
main external API structure.
Definition: avcodec.h:1044
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:223
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
int extradata_size
Definition: avcodec.h:1159
int coded_height
Definition: avcodec.h:1228
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
#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:339
static uint32_t state
Definition: trasher.c:27
MpegEncContext.
Definition: mpegvideo.h:204
struct AVCodecContext * avctx
Definition: mpegvideo.h:221
common internal api header.
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:3694
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.