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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
|
/*
* Copyright (C) 2001-2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* $Id: xine_decoder.c,v 1.51 2002/08/19 17:40:41 guenter Exp $
*
* xine decoder plugin using ffmpeg
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <pthread.h>
#include "xine_internal.h"
#include "video_out.h"
#include "buffer.h"
#include "metronom.h"
#include "xineutils.h"
#include "libavcodec/avcodec.h"
#include "libavcodec/dsputil.h"
/*
#define LOG
*/
#define SLICE_BUFFER_SIZE (1194 * 1024)
typedef struct ff_decoder_s {
video_decoder_t video_decoder;
xine_t *xine;
vo_instance_t *video_out;
int video_step;
int decoder_ok;
xine_bmiheader bih;
unsigned char *buf;
int bufsize;
int size;
int skipframes;
int illegal_vlc;
AVPicture av_picture;
AVCodecContext context;
/* mpeg sequence header parsing, stolen from libmpeg2 */
uint32_t shift;
uint8_t *chunk_buffer;
uint8_t *chunk_ptr;
uint8_t code;
int is_continous;
} ff_decoder_t;
#define VIDEOBUFSIZE 128*1024
static int ff_can_handle (video_decoder_t *this_gen, int buf_type) {
buf_type &= 0xFFFF0000;
return ( buf_type == BUF_VIDEO_MSMPEG4_V3 ||
buf_type == BUF_VIDEO_MSMPEG4_V2 ||
buf_type == BUF_VIDEO_MSMPEG4_V1 ||
buf_type == BUF_VIDEO_WMV7 ||
buf_type == BUF_VIDEO_MPEG4 ||
buf_type == BUF_VIDEO_XVID ||
buf_type == BUF_VIDEO_DIVX5 ||
buf_type == BUF_VIDEO_MJPEG ||
/* buf_type == BUF_VIDEO_I263 || */
buf_type == BUF_VIDEO_H263 ||
buf_type == BUF_VIDEO_RV10 ||
/* PIX_FMT_YUV410P must be supported to enable svq1 */
/* buf_type == BUF_VIDEO_SORENSON_V1 || */
buf_type == BUF_VIDEO_JPEG ||
buf_type == BUF_VIDEO_MPEG);
}
static void init_codec (ff_decoder_t *this, AVCodec *codec) {
/* force (width % 8 == 0), otherwise there will be
* display problems with Xv.
*/
this->bih.biWidth = (this->bih.biWidth + 7) & (~7);
memset(&this->context, 0, sizeof(this->context));
this->context.width = this->bih.biWidth;
this->context.height = this->bih.biHeight;
if (avcodec_open (&this->context, codec) < 0) {
printf ("ffmpeg: couldn't open decoder\n");
return;
}
this->decoder_ok = 1;
this->video_out->open (this->video_out);
/* needed to play streams generated by MS ISO MPEG4 codec.
Michael Niedermayer explained:
M$ "ISO MPEG4" uses illegal vlc code combinations, a ISO MPEG4 compliant
decoder which support error resilience should handle them like errors.
*/
if (this->illegal_vlc)
this->context.error_resilience=-1;
if (this->buf)
free (this->buf);
this->buf = xine_xmalloc (VIDEOBUFSIZE);
this->bufsize = VIDEOBUFSIZE;
this->skipframes = 0;
}
static void find_sequence_header (ff_decoder_t *this,
uint8_t * current, uint8_t * end){
uint8_t code;
if (this->decoder_ok)
return;
while (current != end) {
uint32_t shift;
uint8_t *chunk_ptr;
uint8_t *limit;
uint8_t byte;
code = this->code;
/* copy chunk */
shift = this->shift;
chunk_ptr = this->chunk_ptr;
limit = current + (this->chunk_buffer + SLICE_BUFFER_SIZE - chunk_ptr);
if (limit > end)
limit = end;
while (1) {
byte = *current++;
if (shift != 0x00000100) {
shift = (shift | byte) << 8;
*chunk_ptr++ = byte;
if (current < limit)
continue;
if (current == end) {
this->chunk_ptr = chunk_ptr;
this->shift = shift;
current = 0;
break;
} else {
/* we filled the chunk buffer without finding a start code */
this->code = 0xb4; /* sequence_error_code */
this->chunk_ptr = this->chunk_buffer;
break;
}
}
this->code = byte;
this->chunk_ptr = this->chunk_buffer;
this->shift = 0xffffff00;
break;
}
if (current == NULL)
return ;
#ifdef LOG
printf ("ffmpeg: looking for sequence header... %02x\n", code);
#endif
/* mpeg2_stats (code, this->chunk_buffer); */
if (code == 0xb3) { /* sequence_header_code */
AVCodec *codec = NULL;
int width, height, frame_rate_code;
#ifdef LOG
printf ("ffmpeg: found sequence header !\n");
#endif
height = (this->chunk_buffer[0] << 16) | (this->chunk_buffer[1] << 8)
| this->chunk_buffer[2];
width = ((height >> 12) + 15) & ~15;
height = ((height & 0xfff) + 15) & ~15;
xine_log (this->xine, XINE_LOG_FORMAT,
"ffmpeg: frame size is %d x %d\n",
width, height);
this->bih.biWidth = width;
this->bih.biHeight = height;
frame_rate_code = this->chunk_buffer[3] & 15;
switch (frame_rate_code) {
case 1: /* 23.976 fps */
this->video_step = 3913;
break;
case 2: /* 24 fps */
this->video_step = 3750;
break;
case 3: /* 25 fps */
this->video_step = 3600;
break;
case 4: /* 29.97 fps */
this->video_step = 3003;
break;
case 5: /* 30 fps */
this->video_step = 3000;
break;
case 6: /* 50 fps */
this->video_step = 1800;
break;
case 7: /* 59.94 fps */
this->video_step = 1525;
break;
case 8: /* 60 fps */
this->video_step = 1509;
break;
default:
printf ("ffmpeg: invalid/unknown frame rate code : %d \n",
frame_rate_code);
this->video_step = 3000;
}
/*
* init codec
*/
codec = avcodec_find_decoder (CODEC_ID_MPEG1VIDEO);
if (!codec) {
printf ("avcodec_find_decoder (CODEC_ID_MPEG1VIDEO) failed.\n");
abort();
}
this->is_continous = 1;
init_codec (this, codec);
}
}
}
static void ff_init (video_decoder_t *this_gen, vo_instance_t *video_out) {
ff_decoder_t *this = (ff_decoder_t *) this_gen;
this->video_out = video_out;
this->decoder_ok = 0;
this->buf = NULL;
this->shift = 0xffffff00;
this->code = 0xb4;
this->chunk_ptr = this->chunk_buffer;
this->is_continous = 0;
}
static void ff_decode_data (video_decoder_t *this_gen, buf_element_t *buf) {
ff_decoder_t *this = (ff_decoder_t *) this_gen;
int ratio;
#ifdef LOG
printf ("ffmpeg: processing packet type = %08x, buf : %d, buf->decoder_flags=%08x\n",
buf->type, buf, buf->decoder_flags);
#endif
if (buf->decoder_flags & BUF_FLAG_PREVIEW) {
if ( (buf->type & 0xFFFF0000) == BUF_VIDEO_MPEG ) {
find_sequence_header (this, buf->content, buf->content+buf->size);
}
return;
}
if (buf->decoder_flags & BUF_FLAG_HEADER) {
AVCodec *codec = NULL;
int codec_type;
/* init package containing bih */
memcpy ( &this->bih, buf->content, sizeof (xine_bmiheader));
this->video_step = buf->decoder_info[1];
/* init codec */
codec_type = buf->type & 0xFFFF0000;
switch (codec_type) {
case BUF_VIDEO_MSMPEG4_V1:
codec = avcodec_find_decoder (CODEC_ID_MSMPEG4V1);
break;
case BUF_VIDEO_MSMPEG4_V2:
codec = avcodec_find_decoder (CODEC_ID_MSMPEG4V2);
break;
case BUF_VIDEO_MSMPEG4_V3:
codec = avcodec_find_decoder (CODEC_ID_MSMPEG4);
break;
case BUF_VIDEO_WMV7:
codec = avcodec_find_decoder (CODEC_ID_WMV1);
break;
case BUF_VIDEO_MPEG4 :
case BUF_VIDEO_XVID :
case BUF_VIDEO_DIVX5 :
codec = avcodec_find_decoder (CODEC_ID_MPEG4);
break;
case BUF_VIDEO_JPEG:
case BUF_VIDEO_MJPEG:
codec = avcodec_find_decoder (CODEC_ID_MJPEG);
break;
case BUF_VIDEO_I263:
codec = avcodec_find_decoder (CODEC_ID_H263I);
break;
case BUF_VIDEO_H263:
codec = avcodec_find_decoder (CODEC_ID_H263);
break;
case BUF_VIDEO_RV10:
codec = avcodec_find_decoder (CODEC_ID_RV10);
break;
case BUF_VIDEO_SORENSON_V1:
codec = avcodec_find_decoder (CODEC_ID_SVQ1);
break;
default:
printf ("ffmpeg: unknown video format (buftype: 0x%08X)\n",
buf->type & 0xFFFF0000);
}
if (!codec) {
printf ("ffmpeg: couldn't find decoder\n");
return;
}
/* force (width % 8 == 0), otherwise there will be
* display problems with Xv.
*/
this->bih.biWidth = (this->bih.biWidth + 7) & (~7);
memset(&this->context, 0, sizeof(this->context));
this->context.width = this->bih.biWidth;
this->context.height = this->bih.biHeight;
if (avcodec_open (&this->context, codec) < 0) {
printf ("ffmpeg: couldn't open decoder\n");
return;
}
this->decoder_ok = 1;
this->video_out->open (this->video_out);
/* needed to play streams generated by MS ISO MPEG4 codec.
Michael Niedermayer explained:
M$ "ISO MPEG4" uses illegal vlc code combinations, a ISO MPEG4 compliant
decoder which support error resilience should handle them like errors.
*/
if( this->illegal_vlc )
this->context.error_resilience=-1;
if( this->buf )
free( this->buf );
this->buf = malloc( VIDEOBUFSIZE );
this->bufsize = VIDEOBUFSIZE;
this->skipframes = 0;
} else if (this->decoder_ok) {
if( this->size + buf->size > this->bufsize ) {
this->bufsize = this->size + 2 * buf->size;
printf("ffmpeg: increasing source buffer to %d to avoid overflow.\n",
this->bufsize);
this->buf = realloc( this->buf, this->bufsize );
}
xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size);
this->size += buf->size;
if (buf->decoder_flags & BUF_FLAG_FRAMERATE)
this->video_step = buf->decoder_info[0];
if ( (buf->decoder_flags & BUF_FLAG_FRAME_END) || this->is_continous) {
vo_frame_t *img;
int got_picture, len, y;
uint8_t *dy, *du, *dv, *sy, *su, *sv;
int offset;
/* decode video frame(s) */
/* skip decoding b frames if too late */
this->context.hurry_up = (this->skipframes > 2) ? 1:0;
offset = 0;
while (this->size>0) {
len = avcodec_decode_video (&this->context, &this->av_picture,
&got_picture, &this->buf[offset],
this->size);
if (len<0) {
printf ("ffmpeg: error decompressing frame\n");
this->size=0;
return;
}
this->size -= len;
offset += len;
if (!got_picture) {
printf ("ffmpeg: didn't get a picture, got %d bytes left\n",
this->size);
if (this->size>0)
memmove (this->buf, &this->buf[offset], this->size);
return;
}
#ifdef LOG
printf ("ffmpeg: got a picture\n");
#endif
#ifdef ARCH_X86
emms_c ();
#endif
switch(this->context.aspect_ratio_info) {
case FF_ASPECT_SQUARE:
ratio = XINE_ASPECT_RATIO_SQUARE;
break;
case FF_ASPECT_4_3_625:
case FF_ASPECT_4_3_525:
ratio = XINE_ASPECT_RATIO_4_3;
break;
case FF_ASPECT_16_9_625:
case FF_ASPECT_16_9_525:
ratio = XINE_ASPECT_RATIO_ANAMORPHIC;
break;
default:
ratio = XINE_ASPECT_RATIO_DONT_TOUCH;
}
img = this->video_out->get_frame (this->video_out,
/* this->av_picture.linesize[0], */
this->bih.biWidth,
this->bih.biHeight,
ratio,
IMGFMT_YV12,
VO_BOTH_FIELDS);
img->pts = buf->pts;
buf->pts = 0;
img->duration = this->video_step;
if (len<0 || this->skipframes) {
if( !this->skipframes )
printf ("ffmpeg: error decompressing frame\n");
img->bad_frame = 1;
} else {
img->bad_frame = 0;
dy = img->base[0];
du = img->base[1];
dv = img->base[2];
sy = this->av_picture.data[0];
su = this->av_picture.data[1];
sv = this->av_picture.data[2];
for (y=0; y<this->bih.biHeight; y++) {
xine_fast_memcpy (dy, sy, this->bih.biWidth);
dy += img->pitches[0];
sy += this->av_picture.linesize[0];
}
for (y=0; y<(this->bih.biHeight/2); y++) {
if (this->context.pix_fmt != PIX_FMT_YUV444P) {
xine_fast_memcpy (du, su, this->bih.biWidth/2);
xine_fast_memcpy (dv, sv, this->bih.biWidth/2);
} else {
int x;
uint8_t *src;
uint8_t *dst;
/* subsample */
src = su; dst = du;
for (x=0; x<(this->bih.biWidth/2); x++) {
*dst = *src;
dst++;
src += 2;
}
src = sv; dst = dv;
for (x=0; x<(this->bih.biWidth/2); x++) {
*dst = *src;
dst++;
src += 2;
}
}
du += img->pitches[1];
dv += img->pitches[2];
if (this->context.pix_fmt != PIX_FMT_YUV420P) {
su += 2*this->av_picture.linesize[1];
sv += 2*this->av_picture.linesize[2];
} else {
su += this->av_picture.linesize[1];
sv += this->av_picture.linesize[2];
}
}
if (img->copy) {
int height = img->height;
uint8_t *src[3];
src[0] = img->base[0];
src[1] = img->base[1];
src[2] = img->base[2];
while ((height -= 16) >= 0) {
img->copy(img, src);
src[0] += 16 * img->pitches[0];
src[1] += 8 * img->pitches[1];
src[2] += 8 * img->pitches[2];
}
}
}
this->skipframes = img->draw(img);
if( this->skipframes < 0 )
this->skipframes = 0;
img->free(img);
}
}
}
}
static void ff_flush (video_decoder_t *this_gen) {
}
static void ff_reset (video_decoder_t *this_gen) {
/* seems to handle seeking quite nicelly without any code here */
}
static void ff_close (video_decoder_t *this_gen) {
ff_decoder_t *this = (ff_decoder_t *) this_gen;
if (this->decoder_ok) {
avcodec_close (&this->context);
this->video_out->close(this->video_out);
this->decoder_ok = 0;
}
if (this->buf)
free(this->buf);
this->buf = NULL;
}
static char *ff_get_id(void) {
return "ffmpeg video decoder";
}
void avcodec_register_all(void)
{
static int inited = 0;
if (inited != 0)
return;
inited = 1;
/* decoders */
register_avcodec(&h263_decoder);
register_avcodec(&mpeg4_decoder);
register_avcodec(&msmpeg4v1_decoder);
register_avcodec(&msmpeg4v2_decoder);
register_avcodec(&msmpeg4v3_decoder);
register_avcodec(&wmv1_decoder);
register_avcodec(&wmv2_decoder);
register_avcodec(&mpeg_decoder);
register_avcodec(&h263i_decoder);
register_avcodec(&rv10_decoder);
register_avcodec(&svq1_decoder);
register_avcodec(&mjpeg_decoder);
}
static void init_routine(void) {
avcodec_init();
avcodec_register_all();
}
static void ff_dispose (video_decoder_t *this_gen) {
free (this_gen);
}
video_decoder_t *init_video_decoder_plugin (int iface_version, xine_t *xine) {
ff_decoder_t *this ;
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
if (iface_version != 10) {
printf(_("ffmpeg: plugin doesn't support plugin API version %d.\n"
"ffmpeg: this means there's a version mismatch between xine and this "
"ffmpeg: decoder plugin.\nInstalling current plugins should help.\n"),
iface_version);
return NULL;
}
this = (ff_decoder_t *) malloc (sizeof (ff_decoder_t));
this->video_decoder.interface_version = iface_version;
this->video_decoder.can_handle = ff_can_handle;
this->video_decoder.init = ff_init;
this->video_decoder.decode_data = ff_decode_data;
this->video_decoder.flush = ff_flush;
this->video_decoder.reset = ff_reset;
this->video_decoder.close = ff_close;
this->video_decoder.get_identifier = ff_get_id;
this->video_decoder.priority = 5;
this->video_decoder.dispose = ff_dispose;
this->size = 0;
this->xine = xine;
this->chunk_buffer = xine_xmalloc (SLICE_BUFFER_SIZE + 4);
this->illegal_vlc = xine->config->register_bool (xine->config, "codec.ffmpeg_illegal_vlc", 1,
_("allow illegal vlc codes in mpeg4 streams"), NULL, NULL, NULL);
pthread_once( &once_control, init_routine );
return (video_decoder_t *) this;
}
|