FFmpeg
Loading...
Searching...
No Matches
mpegvideo_parser.c
Go to the documentation of this file.
1/*
2 * MPEG-1 / MPEG-2 video parser
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
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#include "libavutil/avassert.h"
24#include "decode.h"
25#include "parser.h"
26#include "mpeg12.h"
27#include "mpeg12data.h"
28#include "parser_internal.h"
29#include "startcode.h"
30
37
38/**
39 * Find the end of the current frame in the bitstream.
40 * @return the position of the first byte of the next frame, or -1
41 */
42static int mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf,
43 int buf_size, AVCodecParserContext *s)
44{
45 int i;
46 uint32_t state = pc->state;
47
48 /* EOF considered as end of frame */
49 if (buf_size == 0)
50 return 0;
51
52/*
53 0 frame start -> 1/4
54 1 first_SEQEXT -> 0/2
55 2 first field start -> 3/0
56 3 second_SEQEXT -> 2/0
57 4 searching end
58*/
59
60 for (i = 0; i < buf_size; i++) {
62 if (pc->frame_start_found & 1) {
63 if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
65 else if (state == EXT_START_CODE + 2) {
66 if ((buf[i] & 3) == 3)
67 pc->frame_start_found = 0;
68 else
69 pc->frame_start_found = (pc->frame_start_found + 1) & 3;
70 }
71 state++;
72 } else {
73 i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
75 i++;
76 pc->frame_start_found = 4;
77 }
78 if (state == SEQ_END_CODE) {
79 pc->frame_start_found = 0;
80 pc->state = -1;
81 return i + 1;
82 }
83 if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
84 pc->frame_start_found = 0;
85 if (pc->frame_start_found < 4 && state == EXT_START_CODE)
87 if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
89 pc->frame_start_found = 0;
90 pc->state = -1;
91 return i - 3;
92 }
93 }
94 if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
95 ff_fetch_timestamp(s, i - 3, 1, i > 3);
96 }
97 }
98 }
99 pc->state = state;
100 return END_NOT_FOUND;
101}
102
104 AVCodecContext *avctx,
105 const uint8_t *buf, int buf_size)
106{
107 struct MpvParseContext *pc = s->priv_data;
108 const uint8_t *buf_end = buf + buf_size;
109 int bytes_left;
110 int did_set_size=0;
111 int set_dim_ret = 0;
112 int bit_rate = 0;
113 int vbv_delay = 0;
115
116 // number of picture coding extensions (i.e. MPEG2 pictures)
117 // in this packet - should be 1 or 2
118 int nb_pic_ext = 0;
119 // when there are two pictures in the packet this indicates
120 // which field is in the first of them
122
123//FIXME replace the crap with get_bits()
124 while (buf < buf_end) {
125 uint32_t start_code = -1;
126 buf= avpriv_find_start_code(buf, buf_end, &start_code);
127 bytes_left = buf_end - buf;
128 switch(start_code) {
130 if (bytes_left >= 2) {
131 s->pict_type = (buf[1] >> 3) & 7;
132 if (bytes_left >= 4)
133 vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3] >> 3);
134 }
135 break;
136 case SEQ_START_CODE:
137 if (bytes_left >= 7) {
138 int frame_rate_index;
139
140 pc->width = (buf[0] << 4) | (buf[1] >> 4);
141 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
142 if(!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height){
143 set_dim_ret = ff_set_dimensions(avctx, pc->width, pc->height);
144 did_set_size=1;
145 }
147 frame_rate_index = buf[3] & 0xf;
148 pc->frame_rate = avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_index];
149 bit_rate = (buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6);
151 }
152 break;
153 case EXT_START_CODE:
154 if (bytes_left >= 1) {
155 switch (buf[0] >> 4) { // ext_type
156 case 0x1: /* sequence extension */
157 if (bytes_left >= 6) {
158 int horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
159 int vert_size_ext = (buf[2] >> 5) & 3;
160 int bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
161 int frame_rate_ext_n = (buf[5] >> 5) & 3;
162 int frame_rate_ext_d = (buf[5] & 0x1f);
163 pc->progressive_sequence = buf[1] & (1 << 3);
164 avctx->has_b_frames= !(buf[5] >> 7);
165
166 switch ((buf[1] >> 1) & 3) { // chroma_format
167 case 1: pix_fmt = AV_PIX_FMT_YUV420P; break;
168 case 2: pix_fmt = AV_PIX_FMT_YUV422P; break;
169 case 3: pix_fmt = AV_PIX_FMT_YUV444P; break;
170 }
171
172 pc->width = (pc->width & 0xFFF) | (horiz_size_ext << 12);
173 pc->height = (pc->height& 0xFFF) | ( vert_size_ext << 12);
174 bit_rate = (bit_rate&0x3FFFF) | (bit_rate_ext << 18);
175 if(did_set_size)
176 set_dim_ret = ff_set_dimensions(avctx, pc->width, pc->height);
177 avctx->framerate.num = pc->frame_rate.num * (frame_rate_ext_n + 1);
178 avctx->framerate.den = pc->frame_rate.den * (frame_rate_ext_d + 1);
180 }
181 break;
182 case 0x8: /* picture coding extension */
183 if (bytes_left >= 5) {
184 int top_field_first = buf[3] & (1 << 7);
185 int repeat_first_field = buf[3] & (1 << 1);
186 int progressive_frame = buf[4] & (1 << 7);
187
188 /* check if we must repeat the frame */
189 s->repeat_pict = 1;
190 if (repeat_first_field) {
191 if (pc->progressive_sequence) {
192 if (top_field_first)
193 s->repeat_pict = 5;
194 else
195 s->repeat_pict = 3;
196 } else if (progressive_frame) {
197 s->repeat_pict = 2;
198 }
199 }
200
201 if (!pc->progressive_sequence && !progressive_frame) {
202 if (top_field_first)
203 s->field_order = AV_FIELD_TT;
204 else
205 s->field_order = AV_FIELD_BB;
206 } else
207 s->field_order = AV_FIELD_PROGRESSIVE;
208
209 s->picture_structure = buf[2] & 3;
210
211 if (!nb_pic_ext) {
212 // remember parity of the first field for the case
213 // when there are 2 fields in packet
214 switch (s->picture_structure) {
217 }
218 }
219
220 nb_pic_ext++;
221 }
222 break;
223 }
224 }
225 break;
226 case -1:
227 goto the_end;
228 default:
229 /* we stop parsing when we encounter a slice. It ensures
230 that this function takes a negligible amount of time */
233 goto the_end;
234 break;
235 }
236 }
237 the_end:
238 if (set_dim_ret < 0)
239 av_log(avctx, AV_LOG_ERROR, "Failed to set dimensions\n");
240
241 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && bit_rate && bit_rate != 0x3FFFF) {
242 avctx->rc_max_rate = 400LL*bit_rate;
243 }
244 if (bit_rate &&
245 ((avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && bit_rate != 0x3FFFF) || vbv_delay != 0xFFFF)) {
246 avctx->bit_rate = 400LL*bit_rate;
247 }
248
249 if (pix_fmt != AV_PIX_FMT_NONE) {
250 s->format = pix_fmt;
251 s->width = pc->width;
252 s->height = pc->height;
253 s->coded_width = FFALIGN(pc->width, 16);
254 s->coded_height = FFALIGN(pc->height, 16);
255 }
256
257 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO || nb_pic_ext > 1) {
258 s->repeat_pict = 1;
259 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
260 s->field_order = nb_pic_ext > 1 ? first_field : AV_FIELD_PROGRESSIVE;
261 }
262}
263
265 AVCodecContext *avctx,
266 const uint8_t **poutbuf, int *poutbuf_size,
267 const uint8_t *buf, int buf_size)
268{
269 struct MpvParseContext *pc1 = s->priv_data;
270 ParseContext *pc= &pc1->pc;
271 int next;
272
273 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
274 next= buf_size;
275 }else{
276 next = mpeg1_find_frame_end(pc, buf, buf_size, s);
277
278 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
279 *poutbuf = NULL;
280 *poutbuf_size = 0;
281 return buf_size;
282 }
283
284 }
285 /* we have a full frame : we just parse the first few MPEG headers
286 to have the full timing information. The time take by this
287 function should be negligible for uncorrupted streams */
288 mpegvideo_extract_headers(s, avctx, buf, buf_size);
289 ff_dlog(NULL, "pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
290 s->pict_type, av_q2d(avctx->framerate), s->repeat_pict);
291
292 *poutbuf = buf;
293 *poutbuf_size = buf_size;
294 return next;
295}
296
298{
299 s->pict_type = AV_PICTURE_TYPE_NONE; // first frame might be partial
300 return 0;
301}
302
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
#define EXT_START_CODE
Definition cavs.h:40
#define SLICE_MAX_START_CODE
Definition cavs.h:39
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition defs.h:214
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition defs.h:215
@ AV_FIELD_UNKNOWN
Definition defs.h:212
@ AV_FIELD_PROGRESSIVE
Definition defs.h:213
static enum AVPixelFormat pix_fmt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static struct @346255127015250356166251341105367306144006377143 state
@ AV_CODEC_ID_MPEG1VIDEO
Definition codec_id.h:51
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
@ AV_PICTURE_STRUCTURE_FRAME
coded as frame
Definition avcodec.h:2614
@ AV_PICTURE_STRUCTURE_BOTTOM_FIELD
coded as bottom field
Definition avcodec.h:2613
@ AV_PICTURE_STRUCTURE_TOP_FIELD
coded as top field
Definition avcodec.h:2612
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition avutil.h:277
#define av_cold
Definition attributes.h:117
#define FFALIGN(x, a)
Definition macros.h:78
#define PICTURE_START_CODE
Definition mpeg12.h:31
#define SEQ_END_CODE
Definition mpeg12.h:28
#define SLICE_MIN_START_CODE
Definition mpeg12.h:32
#define SEQ_START_CODE
Definition mpeg12.h:29
MPEG-1/2 tables.
const AVRational ff_mpeg12_frame_rate_tab[]
static av_cold int mpegvideo_parse_init(AVCodecParserContext *s)
static void mpegvideo_extract_headers(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
const FFCodecParser ff_mpegvideo_parser
static int mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
Find the end of the current frame in the bitstream.
static int mpegvideo_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
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:213
void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
Fetch timestamps for a specific byte within the current access unit.
Definition parser.c:89
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
Accelerated start code search function for start codes common to MPEG-1/2/4 video,...
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
main external API structure.
Definition avcodec.h:443
int width
picture width / height.
Definition avcodec.h:604
AVRational framerate
Definition avcodec.h:563
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition avcodec.h:709
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int64_t rc_max_rate
maximum bitrate
Definition avcodec.h:1288
enum AVCodecID codec_id
Definition avcodec.h:453
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition avcodec.h:619
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
AVRational frame_rate
uint32_t state
contains the last few bytes in MSB order
Definition parser.h:33
int frame_start_found
Definition parser.h:34
#define ff_dlog(a,...)
#define av_log(a,...)
static int first_field(const struct video_data *s)
Definition v4l2.c:260
static const uint8_t start_code[]