yading@10
|
1 /*
|
yading@10
|
2 * MPEG1 / MPEG2 video parser
|
yading@10
|
3 * Copyright (c) 2000,2001 Fabrice Bellard
|
yading@10
|
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
|
yading@10
|
5 *
|
yading@10
|
6 * This file is part of FFmpeg.
|
yading@10
|
7 *
|
yading@10
|
8 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
9 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
10 * License as published by the Free Software Foundation; either
|
yading@10
|
11 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
12 *
|
yading@10
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
16 * Lesser General Public License for more details.
|
yading@10
|
17 *
|
yading@10
|
18 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
19 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
21 */
|
yading@10
|
22
|
yading@10
|
23 #include "parser.h"
|
yading@10
|
24 #include "mpeg12.h"
|
yading@10
|
25 #include "internal.h"
|
yading@10
|
26
|
yading@10
|
27 struct MpvParseContext {
|
yading@10
|
28 ParseContext pc;
|
yading@10
|
29 AVRational frame_rate;
|
yading@10
|
30 int progressive_sequence;
|
yading@10
|
31 int width, height;
|
yading@10
|
32 };
|
yading@10
|
33
|
yading@10
|
34
|
yading@10
|
35 static void mpegvideo_extract_headers(AVCodecParserContext *s,
|
yading@10
|
36 AVCodecContext *avctx,
|
yading@10
|
37 const uint8_t *buf, int buf_size)
|
yading@10
|
38 {
|
yading@10
|
39 struct MpvParseContext *pc = s->priv_data;
|
yading@10
|
40 const uint8_t *buf_end = buf + buf_size;
|
yading@10
|
41 uint32_t start_code;
|
yading@10
|
42 int frame_rate_index, ext_type, bytes_left;
|
yading@10
|
43 int frame_rate_ext_n, frame_rate_ext_d;
|
yading@10
|
44 int top_field_first, repeat_first_field, progressive_frame;
|
yading@10
|
45 int horiz_size_ext, vert_size_ext, bit_rate_ext;
|
yading@10
|
46 int did_set_size=0;
|
yading@10
|
47 int bit_rate = 0;
|
yading@10
|
48 int vbv_delay = 0;
|
yading@10
|
49 //FIXME replace the crap with get_bits()
|
yading@10
|
50 s->repeat_pict = 0;
|
yading@10
|
51
|
yading@10
|
52 while (buf < buf_end) {
|
yading@10
|
53 start_code= -1;
|
yading@10
|
54 buf= avpriv_find_start_code(buf, buf_end, &start_code);
|
yading@10
|
55 bytes_left = buf_end - buf;
|
yading@10
|
56 switch(start_code) {
|
yading@10
|
57 case PICTURE_START_CODE:
|
yading@10
|
58 if (bytes_left >= 2) {
|
yading@10
|
59 s->pict_type = (buf[1] >> 3) & 7;
|
yading@10
|
60 if (bytes_left >= 4)
|
yading@10
|
61 vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3] >> 3);
|
yading@10
|
62 }
|
yading@10
|
63 break;
|
yading@10
|
64 case SEQ_START_CODE:
|
yading@10
|
65 if (bytes_left >= 7) {
|
yading@10
|
66 pc->width = (buf[0] << 4) | (buf[1] >> 4);
|
yading@10
|
67 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
|
yading@10
|
68 if(!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height){
|
yading@10
|
69 avcodec_set_dimensions(avctx, pc->width, pc->height);
|
yading@10
|
70 did_set_size=1;
|
yading@10
|
71 }
|
yading@10
|
72 frame_rate_index = buf[3] & 0xf;
|
yading@10
|
73 pc->frame_rate.den = avctx->time_base.den = ff_mpeg12_frame_rate_tab[frame_rate_index].num;
|
yading@10
|
74 pc->frame_rate.num = avctx->time_base.num = ff_mpeg12_frame_rate_tab[frame_rate_index].den;
|
yading@10
|
75 bit_rate = (buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6);
|
yading@10
|
76 avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
|
yading@10
|
77 }
|
yading@10
|
78 break;
|
yading@10
|
79 case EXT_START_CODE:
|
yading@10
|
80 if (bytes_left >= 1) {
|
yading@10
|
81 ext_type = (buf[0] >> 4);
|
yading@10
|
82 switch(ext_type) {
|
yading@10
|
83 case 0x1: /* sequence extension */
|
yading@10
|
84 if (bytes_left >= 6) {
|
yading@10
|
85 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
|
yading@10
|
86 vert_size_ext = (buf[2] >> 5) & 3;
|
yading@10
|
87 bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
|
yading@10
|
88 frame_rate_ext_n = (buf[5] >> 5) & 3;
|
yading@10
|
89 frame_rate_ext_d = (buf[5] & 0x1f);
|
yading@10
|
90 pc->progressive_sequence = buf[1] & (1 << 3);
|
yading@10
|
91 avctx->has_b_frames= !(buf[5] >> 7);
|
yading@10
|
92
|
yading@10
|
93 pc->width |=(horiz_size_ext << 12);
|
yading@10
|
94 pc->height |=( vert_size_ext << 12);
|
yading@10
|
95 bit_rate = (bit_rate&0x3FFFF) | (bit_rate_ext << 18);
|
yading@10
|
96 if(did_set_size)
|
yading@10
|
97 avcodec_set_dimensions(avctx, pc->width, pc->height);
|
yading@10
|
98 avctx->time_base.den = pc->frame_rate.den * (frame_rate_ext_n + 1) * 2;
|
yading@10
|
99 avctx->time_base.num = pc->frame_rate.num * (frame_rate_ext_d + 1);
|
yading@10
|
100 avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
|
yading@10
|
101 }
|
yading@10
|
102 break;
|
yading@10
|
103 case 0x8: /* picture coding extension */
|
yading@10
|
104 if (bytes_left >= 5) {
|
yading@10
|
105 top_field_first = buf[3] & (1 << 7);
|
yading@10
|
106 repeat_first_field = buf[3] & (1 << 1);
|
yading@10
|
107 progressive_frame = buf[4] & (1 << 7);
|
yading@10
|
108
|
yading@10
|
109 /* check if we must repeat the frame */
|
yading@10
|
110 s->repeat_pict = 1;
|
yading@10
|
111 if (repeat_first_field) {
|
yading@10
|
112 if (pc->progressive_sequence) {
|
yading@10
|
113 if (top_field_first)
|
yading@10
|
114 s->repeat_pict = 5;
|
yading@10
|
115 else
|
yading@10
|
116 s->repeat_pict = 3;
|
yading@10
|
117 } else if (progressive_frame) {
|
yading@10
|
118 s->repeat_pict = 2;
|
yading@10
|
119 }
|
yading@10
|
120 }
|
yading@10
|
121 }
|
yading@10
|
122 break;
|
yading@10
|
123 }
|
yading@10
|
124 }
|
yading@10
|
125 break;
|
yading@10
|
126 case -1:
|
yading@10
|
127 goto the_end;
|
yading@10
|
128 default:
|
yading@10
|
129 /* we stop parsing when we encounter a slice. It ensures
|
yading@10
|
130 that this function takes a negligible amount of time */
|
yading@10
|
131 if (start_code >= SLICE_MIN_START_CODE &&
|
yading@10
|
132 start_code <= SLICE_MAX_START_CODE)
|
yading@10
|
133 goto the_end;
|
yading@10
|
134 break;
|
yading@10
|
135 }
|
yading@10
|
136 }
|
yading@10
|
137 the_end: ;
|
yading@10
|
138 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && bit_rate) {
|
yading@10
|
139 avctx->rc_max_rate = 400*bit_rate;
|
yading@10
|
140 } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && bit_rate &&
|
yading@10
|
141 (bit_rate != 0x3FFFF || vbv_delay != 0xFFFF)) {
|
yading@10
|
142 avctx->bit_rate = 400*bit_rate;
|
yading@10
|
143 }
|
yading@10
|
144 }
|
yading@10
|
145
|
yading@10
|
146 static int mpegvideo_parse(AVCodecParserContext *s,
|
yading@10
|
147 AVCodecContext *avctx,
|
yading@10
|
148 const uint8_t **poutbuf, int *poutbuf_size,
|
yading@10
|
149 const uint8_t *buf, int buf_size)
|
yading@10
|
150 {
|
yading@10
|
151 struct MpvParseContext *pc1 = s->priv_data;
|
yading@10
|
152 ParseContext *pc= &pc1->pc;
|
yading@10
|
153 int next;
|
yading@10
|
154
|
yading@10
|
155 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
|
yading@10
|
156 next= buf_size;
|
yading@10
|
157 }else{
|
yading@10
|
158 next= ff_mpeg1_find_frame_end(pc, buf, buf_size, s);
|
yading@10
|
159
|
yading@10
|
160 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
|
yading@10
|
161 *poutbuf = NULL;
|
yading@10
|
162 *poutbuf_size = 0;
|
yading@10
|
163 return buf_size;
|
yading@10
|
164 }
|
yading@10
|
165
|
yading@10
|
166 }
|
yading@10
|
167 /* we have a full frame : we just parse the first few MPEG headers
|
yading@10
|
168 to have the full timing information. The time take by this
|
yading@10
|
169 function should be negligible for uncorrupted streams */
|
yading@10
|
170 mpegvideo_extract_headers(s, avctx, buf, buf_size);
|
yading@10
|
171 av_dlog(NULL, "pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
|
yading@10
|
172 s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
|
yading@10
|
173
|
yading@10
|
174 *poutbuf = buf;
|
yading@10
|
175 *poutbuf_size = buf_size;
|
yading@10
|
176 return next;
|
yading@10
|
177 }
|
yading@10
|
178
|
yading@10
|
179 static int mpegvideo_split(AVCodecContext *avctx,
|
yading@10
|
180 const uint8_t *buf, int buf_size)
|
yading@10
|
181 {
|
yading@10
|
182 int i;
|
yading@10
|
183 uint32_t state= -1;
|
yading@10
|
184 int found=0;
|
yading@10
|
185
|
yading@10
|
186 for(i=0; i<buf_size; i++){
|
yading@10
|
187 state= (state<<8) | buf[i];
|
yading@10
|
188 if(state == 0x1B3){
|
yading@10
|
189 found=1;
|
yading@10
|
190 }else if(found && state != 0x1B5 && state < 0x200 && state >= 0x100)
|
yading@10
|
191 return i-3;
|
yading@10
|
192 }
|
yading@10
|
193 return 0;
|
yading@10
|
194 }
|
yading@10
|
195
|
yading@10
|
196 static int mpegvideo_parse_init(AVCodecParserContext *s)
|
yading@10
|
197 {
|
yading@10
|
198 s->pict_type = AV_PICTURE_TYPE_NONE; // first frame might be partial
|
yading@10
|
199 return 0;
|
yading@10
|
200 }
|
yading@10
|
201
|
yading@10
|
202 AVCodecParser ff_mpegvideo_parser = {
|
yading@10
|
203 .codec_ids = { AV_CODEC_ID_MPEG1VIDEO, AV_CODEC_ID_MPEG2VIDEO },
|
yading@10
|
204 .priv_data_size = sizeof(struct MpvParseContext),
|
yading@10
|
205 .parser_init = mpegvideo_parse_init,
|
yading@10
|
206 .parser_parse = mpegvideo_parse,
|
yading@10
|
207 .parser_close = ff_parse_close,
|
yading@10
|
208 .split = mpegvideo_split,
|
yading@10
|
209 };
|