[4a1d00]: src / combined / ffmpeg / ff_mpeg_parser.c  Maximize  Restore  History

Download this file

322 lines (276 with data), 8.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright (C) 2001-2004 the xine project
*
* This file is part of xine, a free video player.
*
* xine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* xine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
*
* Simple MPEG-ES parser/framer by Thibaut Mattern (tmattern@noos.fr)
* based on libmpeg2 decoder.
*/
#define LOG_MODULE "mpeg_parser"
#define LOG_VERBOSE
/*
#define LOG
*/
#include "ff_mpeg_parser.h"
/* mpeg frame rate table from lavc */
static const int frame_rate_tab[][2] = {
{ 0, 0},
{24000, 1001},
{ 24, 1},
{ 25, 1},
{30000, 1001},
{ 30, 1},
{ 50, 1},
{60000, 1001},
{ 60, 1},
/* Xing's 15fps: (9) */
{ 15, 1},
/* libmpeg3's "Unofficial economy rates": (10-13) */
{ 5, 1},
{ 10, 1},
{ 12, 1},
{ 15, 1},
{ 0, 0},
};
void mpeg_parser_init (mpeg_parser_t *parser)
{
parser->chunk_buffer = malloc(BUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
mpeg_parser_reset(parser);
}
void mpeg_parser_dispose (mpeg_parser_t *parser)
{
if ( parser == NULL ) return;
free(parser->chunk_buffer);
}
void mpeg_parser_reset (mpeg_parser_t *parser)
{
parser->shift = 0xffffff00;
parser->is_sequence_needed = 1;
parser->in_slice = 0;
parser->chunk_ptr = parser->chunk_buffer;
parser->chunk_start = parser->chunk_buffer;
parser->buffer_size = 0;
parser->code = 0xb4;
parser->picture_coding_type = 0;
parser->width = 0;
parser->height = 0;
parser->rate_code = 0;
parser->aspect_ratio_info = 0;
parser->frame_duration = 0;
parser->is_mpeg1 = 0;
parser->has_sequence = 0;
parser->frame_aspect_ratio = 0.0;
}
static void parse_header_picture (mpeg_parser_t *parser, uint8_t * buffer)
{
parser->picture_coding_type = (buffer [1] >> 3) & 7;
}
static double get_aspect_ratio(mpeg_parser_t *parser)
{
double ratio;
double mpeg1_pel_ratio[16] = {1.0 /* forbidden */,
1.0, 0.6735, 0.7031, 0.7615, 0.8055, 0.8437, 0.8935, 0.9157,
0.9815, 1.0255, 1.0695, 1.0950, 1.1575, 1.2015, 1.0 /*reserved*/ };
if( !parser->is_mpeg1 ) {
/* these hardcoded values are defined on mpeg2 standard for
* aspect ratio. other values are reserved or forbidden. */
switch (parser->aspect_ratio_info) {
case 2:
ratio = 4.0 / 3.0;
break;
case 3:
ratio = 16.0 / 9.0;
break;
case 4:
ratio = 2.11 / 1.0;
break;
case 1:
default:
ratio = (double)parser->width / (double)parser->height;
break;
}
} else {
/* mpeg1 constants refer to pixel aspect ratio */
ratio = (double)parser->width / (double)parser->height;
ratio /= mpeg1_pel_ratio[parser->aspect_ratio_info];
}
return ratio;
}
static int parse_chunk (mpeg_parser_t *parser, int code, uint8_t *buffer, int len)
{
int is_frame_done;
int next_code = parser->code;
/* wait for sequence_header_code */
if (parser->is_sequence_needed) {
if (code != 0xb3) {
lprintf("waiting for sequence header\n");
parser->chunk_ptr = parser->chunk_buffer;
return 0;
}
}
is_frame_done = parser->in_slice && ((!next_code) || (next_code == 0xb7));
if (is_frame_done)
parser->in_slice = 0;
switch (code) {
case 0x00: /* picture_start_code */
parse_header_picture (parser, buffer);
parser->in_slice = 1;
switch (parser->picture_coding_type) {
case B_TYPE:
lprintf ("B-Frame\n");
break;
case P_TYPE:
lprintf ("P-Frame\n");
break;
case I_TYPE:
lprintf ("I-Frame\n");
break;
}
break;
case 0xb2: /* user data code */
/* process_userdata(mpeg2dec, buffer); */
break;
case 0xb3: /* sequence_header_code */
{
int value;
uint16_t width, height;
if (parser->is_sequence_needed) {
parser->is_sequence_needed = 0;
}
if ((buffer[6] & 0x20) != 0x20) {
lprintf("Invalid sequence: missing marker_bit\n");
parser->has_sequence = 0;
break; /* missing marker_bit */
}
value = (buffer[0] << 16) |
(buffer[1] << 8) |
buffer[2];
width = ((value >> 12) + 15) & ~15;
height = ((value & 0xfff) + 15) & ~15;
if ((width > 1920) || (height > 1152)) {
lprintf("Invalid sequence: width=%d, height=%d\n", width, height);
parser->has_sequence = 0;
break; /* size restrictions for MP@HL */
}
parser->width = width;
parser->height = height;
parser->rate_code = buffer[3] & 15;
parser->aspect_ratio_info = buffer[3] >> 4;
if (parser->rate_code < (sizeof(frame_rate_tab)/sizeof(*frame_rate_tab))) {
parser->frame_duration = 90000;
parser->frame_duration *= frame_rate_tab[parser->rate_code][1];
parser->frame_duration /= frame_rate_tab[parser->rate_code][0];
} else {
printf ("invalid/unknown frame rate code : %d \n",
parser->rate_code);
parser->frame_duration = 0;
}
parser->has_sequence = 1;
parser->is_mpeg1 = 1;
}
break;
case 0xb5: /* extension_start_code */
switch (buffer[0] & 0xf0) {
case 0x10: /* sequence extension */
parser->is_mpeg1 = 0;
}
default:
if (code >= 0xb9)
lprintf ("stream not demultiplexed ?\n");
if (code >= 0xb0)
break;
}
return is_frame_done;
}
static inline uint8_t *copy_chunk (mpeg_parser_t *parser,
uint8_t *current, uint8_t *end)
{
uint32_t shift;
uint8_t *chunk_ptr;
uint8_t *limit;
uint8_t byte;
shift = parser->shift;
chunk_ptr = parser->chunk_ptr;
limit = current + (parser->chunk_buffer + BUFFER_SIZE - chunk_ptr);
if (limit > end)
limit = end;
while (1) {
byte = *current++;
*chunk_ptr++ = byte;
if (shift != 0x00000100) {
shift = (shift | byte) << 8;
if (current < limit)
continue;
if (current == end) {
parser->chunk_ptr = chunk_ptr;
parser->shift = shift;
lprintf("Need more bytes\n");
return NULL;
} else {
/* we filled the chunk buffer without finding a start code */
lprintf("Buffer full\n");
parser->code = 0xb4; /* sequence_error_code */
parser->chunk_ptr = parser->chunk_buffer;
return current;
}
}
lprintf("New chunk: 0x%2X\n", byte);
parser->chunk_ptr = chunk_ptr;
parser->shift = 0xffffff00;
parser->code = byte;
return current;
}
}
uint8_t *mpeg_parser_decode_data (mpeg_parser_t *parser,
uint8_t *current, uint8_t *end,
int *flush)
{
int ret;
uint8_t code;
ret = 0;
*flush = 0;
while (current != end) {
if (parser->chunk_ptr == parser->chunk_buffer) {
/* write the beginning of the chunk */
parser->chunk_buffer[0] = 0x00;
parser->chunk_buffer[1] = 0x00;
parser->chunk_buffer[2] = 0x01;
parser->chunk_buffer[3] = parser->code;
parser->chunk_ptr += 4;
parser->chunk_start = parser->chunk_ptr;
parser->has_sequence = 0;
}
code = parser->code;
current = copy_chunk (parser, current, end);
if (current == NULL)
return NULL;
ret = parse_chunk (parser, code, parser->chunk_start,
parser->chunk_ptr - parser->chunk_start - 4);
parser->chunk_start = parser->chunk_ptr;
if (ret == 1) {
if (parser->has_sequence) {
parser->frame_aspect_ratio = get_aspect_ratio(parser);
}
parser->buffer_size = parser->chunk_ptr - parser->chunk_buffer - 4;
parser->chunk_ptr = parser->chunk_buffer;
if (parser->code == 0xb7) /* sequence end, maybe a still menu */
*flush = 1;
return current;
}
}
return NULL;
}