From 3b6ef82b64306c9d03341fe60e5ad641ad50809d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20=27Flameeyes=27=20Petten=C3=B2?= Date: Wed, 4 Apr 2007 21:07:48 +0200 Subject: Rename FFmpeg plugins' sources so that there are no conflicts with other source files. This way when you get a backtrace, video_decoder.c is never the FFmpeg one. --HG-- rename : src/libffmpeg/audio_decoder.c => src/libffmpeg/ff_audio_decoder.c rename : src/libffmpeg/dvaudio_decoder.c => src/libffmpeg/ff_dvaudio_decoder.c rename : src/libffmpeg/mpeg_parser.c => src/libffmpeg/ff_mpeg_parser.c rename : src/libffmpeg/mpeg_parser.h => src/libffmpeg/ff_mpeg_parser.h rename : src/libffmpeg/video_decoder.c => src/libffmpeg/ff_video_decoder.c rename : src/libffmpeg/xine_decoder.c => src/libffmpeg/ffmpeg_decoder.c rename : src/libffmpeg/xine_decoder.h => src/libffmpeg/ffmpeg_decoder.h rename : src/libffmpeg/xine_encoder.c => src/libffmpeg/ffmpeg_encoder.c --- src/libffmpeg/Makefile.am | 21 +- src/libffmpeg/audio_decoder.c | 554 ----------- src/libffmpeg/dvaudio_decoder.c | 433 --------- src/libffmpeg/ff_audio_decoder.c | 554 +++++++++++ src/libffmpeg/ff_dvaudio_decoder.c | 433 +++++++++ src/libffmpeg/ff_mpeg_parser.c | 323 +++++++ src/libffmpeg/ff_mpeg_parser.h | 83 ++ src/libffmpeg/ff_video_decoder.c | 1772 ++++++++++++++++++++++++++++++++++++ src/libffmpeg/ffmpeg_decoder.c | 329 +++++++ src/libffmpeg/ffmpeg_decoder.h | 56 ++ src/libffmpeg/ffmpeg_encoder.c | 334 +++++++ src/libffmpeg/mpeg_parser.c | 323 ------- src/libffmpeg/mpeg_parser.h | 83 -- src/libffmpeg/video_decoder.c | 1772 ------------------------------------ src/libffmpeg/xine_decoder.c | 329 ------- src/libffmpeg/xine_decoder.h | 56 -- src/libffmpeg/xine_encoder.c | 334 ------- 17 files changed, 3893 insertions(+), 3896 deletions(-) delete mode 100644 src/libffmpeg/audio_decoder.c delete mode 100644 src/libffmpeg/dvaudio_decoder.c create mode 100644 src/libffmpeg/ff_audio_decoder.c create mode 100644 src/libffmpeg/ff_dvaudio_decoder.c create mode 100644 src/libffmpeg/ff_mpeg_parser.c create mode 100644 src/libffmpeg/ff_mpeg_parser.h create mode 100644 src/libffmpeg/ff_video_decoder.c create mode 100644 src/libffmpeg/ffmpeg_decoder.c create mode 100644 src/libffmpeg/ffmpeg_decoder.h create mode 100644 src/libffmpeg/ffmpeg_encoder.c delete mode 100644 src/libffmpeg/mpeg_parser.c delete mode 100644 src/libffmpeg/mpeg_parser.h delete mode 100644 src/libffmpeg/video_decoder.c delete mode 100644 src/libffmpeg/xine_decoder.c delete mode 100644 src/libffmpeg/xine_decoder.h delete mode 100644 src/libffmpeg/xine_encoder.c diff --git a/src/libffmpeg/Makefile.am b/src/libffmpeg/Makefile.am index 5f08ea20e..1efc77327 100644 --- a/src/libffmpeg/Makefile.am +++ b/src/libffmpeg/Makefile.am @@ -20,29 +20,26 @@ EXTRA_DIST = xine_encoder.c diff_to_ffmpeg_cvs.txt INTERNAL_DOCS = diff_to_ffmpeg_cvs.txt -libdir = $(XINE_PLUGINDIR) - -lib_LTLIBRARIES = xineplug_decode_ff.la xineplug_decode_dvaudio.la +xineplug_LTLIBRARIES = xineplug_decode_ff.la xineplug_decode_dvaudio.la if HAVE_DXR3 AM_CPPFLAGS = -I$(top_srcdir)/src/dxr3 $(X_CFLAGS) $(ff_cppflags) \ $(ZLIB_CPPFLAGS) -xineplug_decode_ff_la_SOURCES = xine_decoder.c audio_decoder.c video_decoder.c \ - xine_encoder.c mpeg_parser.c +xineplug_decode_ff_la_SOURCES = ffmpeg_decoder.c ff_audio_decoder.c ff_video_decoder.c \ + ffmpeg_encoder.c ff_mpeg_parser.c ffmpeg_decoder.h \ + ff_mpeg_parser.h else AM_CPPFLAGS = $(ff_cppflags) $(ZLIB_CPPFLAGS) -xineplug_decode_ff_la_SOURCES = xine_decoder.c audio_decoder.c video_decoder.c \ - mpeg_parser.c +xineplug_decode_ff_la_SOURCES = ffmpeg_decoder.c ff_audio_decoder.c ff_video_decoder.c \ + ff_mpeg_parser.c ffmpeg_decoder.h ff_mpeg_parser.h endif xineplug_decode_ff_la_CFLAGS = $(VISIBILITY_FLAG) $(AM_CFLAGS) -xineplug_decode_ff_la_LDFLAGS = -avoid-version -module @IMPURE_TEXT_LDFLAGS@ +xineplug_decode_ff_la_LDFLAGS = $(xineplug_ldflags) $(IMPURE_TEXT_LDFLAGS) xineplug_decode_ff_la_LIBADD = $(MLIB_LIBS) $(XINE_LIB) -lm $(ZLIB_LIBS) \ $(link_ffmpeg) $(PTHREAD_LIBS) xineplug_decode_dvaudio_la_CFLAGS = $(VISIBILITY_FLAG) $(AM_CFLAGS) -xineplug_decode_dvaudio_la_LDFLAGS = -avoid-version -module -xineplug_decode_dvaudio_la_SOURCES = dvaudio_decoder.c +xineplug_decode_dvaudio_la_LDFLAGS = $(xineplug_ldflags) +xineplug_decode_dvaudio_la_SOURCES = ff_dvaudio_decoder.c xineplug_decode_dvaudio_la_LIBADD = $(XINE_LIB) - -noinst_HEADERS = xine_decoder.h mpeg_parser.h diff --git a/src/libffmpeg/audio_decoder.c b/src/libffmpeg/audio_decoder.c deleted file mode 100644 index 7ab1f56b4..000000000 --- a/src/libffmpeg/audio_decoder.c +++ /dev/null @@ -1,554 +0,0 @@ -/* - * Copyright (C) 2001-2005 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: audio_decoder.c,v 1.33 2007/01/28 18:38:33 miguelfreitas Exp $ - * - * xine audio decoder plugin using ffmpeg - * - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#include "ffmpeg_config.h" -#endif - -#include -#include -#include -#include -#include -#include - -#define LOG_MODULE "ffmpeg_audio_dec" -#define LOG_VERBOSE -/* -#define LOG -*/ - -#include "xine_internal.h" -#include "buffer.h" -#include "xineutils.h" -#include "bswap.h" -#include "xine_decoder.h" - -#define AUDIOBUFSIZE (64 * 1024) - -typedef struct { - audio_decoder_class_t decoder_class; -} ff_audio_class_t; - -typedef struct ff_audio_decoder_s { - audio_decoder_t audio_decoder; - - xine_stream_t *stream; - - int output_open; - int audio_channels; - int audio_bits; - int audio_sample_rate; - - unsigned char *buf; - int bufsize; - int size; - - AVCodecContext *context; - AVCodec *codec; - - char *decode_buffer; - int decoder_ok; - -} ff_audio_decoder_t; - - -static const ff_codec_t ff_audio_lookup[] = { - {BUF_AUDIO_WMAV1, CODEC_ID_WMAV1, "MS Windows Media Audio 1 (ffmpeg)"}, - {BUF_AUDIO_WMAV2, CODEC_ID_WMAV2, "MS Windows Media Audio 2 (ffmpeg)"}, - {BUF_AUDIO_14_4, CODEC_ID_RA_144, "Real 14.4 (ffmpeg)"}, - {BUF_AUDIO_28_8, CODEC_ID_RA_288, "Real 28.8 (ffmpeg)"}, - {BUF_AUDIO_MPEG, CODEC_ID_MP3, "MP3 (ffmpeg)"}, - {BUF_AUDIO_MSADPCM, CODEC_ID_ADPCM_MS, "MS ADPCM (ffmpeg)"}, - {BUF_AUDIO_QTIMAADPCM, CODEC_ID_ADPCM_IMA_QT, "QT IMA ADPCM (ffmpeg)"}, - {BUF_AUDIO_MSIMAADPCM, CODEC_ID_ADPCM_IMA_WAV, "MS IMA ADPCM (ffmpeg)"}, - {BUF_AUDIO_DK3ADPCM, CODEC_ID_ADPCM_IMA_DK3, "Duck DK3 ADPCM (ffmpeg)"}, - {BUF_AUDIO_DK4ADPCM, CODEC_ID_ADPCM_IMA_DK4, "Duck DK4 ADPCM (ffmpeg)"}, - {BUF_AUDIO_VQA_IMA, CODEC_ID_ADPCM_IMA_WS, "Westwood Studios IMA (ffmpeg)"}, - {BUF_AUDIO_SMJPEG_IMA, CODEC_ID_ADPCM_IMA_SMJPEG, "SMJPEG IMA (ffmpeg)"}, - {BUF_AUDIO_XA_ADPCM, CODEC_ID_ADPCM_XA, "CD-ROM/XA ADPCM (ffmpeg)"}, - {BUF_AUDIO_4X_ADPCM, CODEC_ID_ADPCM_4XM, "4X ADPCM (ffmpeg)"}, - {BUF_AUDIO_EA_ADPCM, CODEC_ID_ADPCM_EA, "Electronic Arts ADPCM (ffmpeg)"}, - {BUF_AUDIO_MULAW, CODEC_ID_PCM_MULAW, "mu-law logarithmic PCM (ffmpeg)"}, - {BUF_AUDIO_ALAW, CODEC_ID_PCM_ALAW, "A-law logarithmic PCM (ffmpeg)"}, - {BUF_AUDIO_ROQ, CODEC_ID_ROQ_DPCM, "RoQ DPCM (ffmpeg)"}, - {BUF_AUDIO_INTERPLAY, CODEC_ID_INTERPLAY_DPCM, "Interplay DPCM (ffmpeg)"}, - {BUF_AUDIO_MAC3, CODEC_ID_MACE3, "MACE 3:1 (ffmpeg)"}, - {BUF_AUDIO_MAC6, CODEC_ID_MACE6, "MACE 6:1 (ffmpeg)"}, - {BUF_AUDIO_XAN_DPCM, CODEC_ID_XAN_DPCM, "Origin Xan DPCM (ffmpeg)"}, - {BUF_AUDIO_VMD, CODEC_ID_VMDAUDIO, "Sierra VMD Audio (ffmpeg)"}, - {BUF_AUDIO_FLAC, CODEC_ID_FLAC, "FLAC (ffmpeg)"}, - {BUF_AUDIO_SHORTEN, CODEC_ID_SHORTEN, "Shorten (ffmpeg)"}, - {BUF_AUDIO_ALAC, CODEC_ID_ALAC, "ALAC (ffmpeg)"}, - {BUF_AUDIO_QDESIGN2, CODEC_ID_QDM2, "QDesign (ffmpeg)"}, - {BUF_AUDIO_COOK, CODEC_ID_COOK, "RealAudio Cooker (ffmpeg)"}, - {BUF_AUDIO_TRUESPEECH, CODEC_ID_TRUESPEECH, "TrueSpeech (ffmpeg)"}, - {BUF_AUDIO_TTA, CODEC_ID_TTA, "True Audio Lossless (ffmpeg)"}, - {BUF_AUDIO_SMACKER, CODEC_ID_SMACKAUDIO, "Smacker (ffmpeg)"}, - {BUF_AUDIO_FLVADPCM, CODEC_ID_ADPCM_SWF, "Flash ADPCM (ffmpeg)"}, - {BUF_AUDIO_WAVPACK, CODEC_ID_WAVPACK, "WavPack (ffmpeg)"}, -}; - - - static void ff_audio_ensure_buffer_size(ff_audio_decoder_t *this, int size) { - if (size > this->bufsize) { - this->bufsize = size + size / 2; - xprintf(this->stream->xine, XINE_VERBOSITY_LOG, - _("ffmpeg_audio_dec: increasing buffer to %d to avoid overflow.\n"), - this->bufsize); - this->buf = realloc( this->buf, this->bufsize ); - } -} - -static void ff_audio_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { - - ff_audio_decoder_t *this = (ff_audio_decoder_t *) this_gen; - int bytes_consumed; - int decode_buffer_size; - int offset; - int out; - audio_buffer_t *audio_buffer; - int bytes_to_send; - - if ( (buf->decoder_flags & BUF_FLAG_HEADER) && - !(buf->decoder_flags & BUF_FLAG_SPECIAL) ) { - - /* accumulate init data */ - ff_audio_ensure_buffer_size(this, this->size + buf->size); - memcpy(this->buf + this->size, buf->content, buf->size); - this->size += buf->size; - - if (buf->decoder_flags & BUF_FLAG_FRAME_END) { - size_t i; - unsigned int codec_type; - xine_waveformatex *audio_header; - - codec_type = buf->type & 0xFFFF0000; - this->codec = NULL; - - for(i = 0; i < sizeof(ff_audio_lookup)/sizeof(ff_codec_t); i++) - if(ff_audio_lookup[i].type == codec_type) { - pthread_mutex_lock (&ffmpeg_lock); - this->codec = avcodec_find_decoder(ff_audio_lookup[i].id); - pthread_mutex_unlock (&ffmpeg_lock); - _x_meta_info_set(this->stream, XINE_META_INFO_AUDIOCODEC, - ff_audio_lookup[i].name); - break; - } - - if (!this->codec) { - xprintf (this->stream->xine, XINE_VERBOSITY_LOG, - _("ffmpeg_audio_dec: couldn't find ffmpeg decoder for buf type 0x%X\n"), - codec_type); - _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_HANDLED, 0); - return; - } - - this->context = avcodec_alloc_context(); - - if(buf->decoder_flags & BUF_FLAG_STDHEADER) { - this->audio_sample_rate = buf->decoder_info[1]; - this->audio_channels = buf->decoder_info[3]; - - if(this->size) { - audio_header = (xine_waveformatex *)this->buf; - - this->context->block_align = audio_header->nBlockAlign; - this->context->bit_rate = audio_header->nAvgBytesPerSec * 8; - - if(audio_header->cbSize > 0) { - this->context->extradata = xine_xmalloc(audio_header->cbSize); - this->context->extradata_size = audio_header->cbSize; - memcpy( this->context->extradata, - (uint8_t *)audio_header + sizeof(xine_waveformatex), - audio_header->cbSize ); - } - } - } else { - short *ptr; - - switch(codec_type) { - case BUF_AUDIO_14_4: - this->audio_sample_rate = 8000; - this->audio_channels = 1; - - this->context->block_align = 240; - break; - case BUF_AUDIO_28_8: - this->audio_sample_rate = BE_16(&this->buf[0x30]); - this->audio_channels = this->buf[0x37]; - /* this->audio_bits = buf->content[0x35] */ - - this->context->block_align = BE_16(&this->buf[0x2A]); - - this->context->extradata_size = 5*sizeof(short); - this->context->extradata = xine_xmalloc(this->context->extradata_size); - - ptr = (short *) this->context->extradata; - - ptr[0] = BE_16(&this->buf[0x2C]); /* subpacket size */ - ptr[1] = BE_16(&this->buf[0x28]); /* subpacket height */ - ptr[2] = BE_16(&this->buf[0x16]); /* subpacket flavour */ - ptr[3] = BE_32(&this->buf[0x18]); /* coded frame size */ - ptr[4] = 0; /* codec's data length */ - break; - default: - xprintf(this->stream->xine, XINE_VERBOSITY_LOG, - "ffmpeg_audio_dec: unknown header with buf type 0x%X\n", codec_type); - break; - } - } - - /* Current ffmpeg audio decoders always use 16 bits/sample - * buf->decoder_info[2] can't be used as it doesn't refer to the output - * bits/sample for some codecs (e.g. MS ADPCM) */ - this->audio_bits = 16; - - this->context->bits_per_sample = this->audio_bits; - this->context->sample_rate = this->audio_sample_rate; - this->context->channels = this->audio_channels; - this->context->codec_id = this->codec->id; - this->context->codec_tag = _x_stream_info_get(this->stream, XINE_STREAM_INFO_AUDIO_FOURCC); - - this->size = 0; - - this->decode_buffer = xine_xmalloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); - - return; - } - } else if ((buf->decoder_flags & BUF_FLAG_SPECIAL) && - (buf->decoder_info[1] == BUF_SPECIAL_STSD_ATOM)) { - - this->context->extradata_size = buf->decoder_info[2]; - this->context->extradata = xine_xmalloc(buf->decoder_info[2] + - FF_INPUT_BUFFER_PADDING_SIZE); - memcpy(this->context->extradata, buf->decoder_info_ptr[2], - buf->decoder_info[2]); - - } else if (!(buf->decoder_flags & BUF_FLAG_SPECIAL)) { - - if( !this->decoder_ok ) { - if ( ! this->context || ! this->codec ) { - xprintf (this->stream->xine, XINE_VERBOSITY_LOG, - _("ffmpeg_audio_dec: trying to open null codec\n")); - _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_HANDLED, 0); - return; - } - - pthread_mutex_lock (&ffmpeg_lock); - if (avcodec_open (this->context, this->codec) < 0) { - pthread_mutex_unlock (&ffmpeg_lock); - xprintf (this->stream->xine, XINE_VERBOSITY_LOG, - _("ffmpeg_audio_dec: couldn't open decoder\n")); - _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_HANDLED, 0); - return; - } - pthread_mutex_unlock (&ffmpeg_lock); - this->decoder_ok = 1; - } - - if (!this->output_open) { - this->output_open = this->stream->audio_out->open(this->stream->audio_out, - this->stream, this->audio_bits, this->audio_sample_rate, - _x_ao_channels2mode(this->audio_channels)); - } - - /* if the audio still isn't open, bail */ - if (!this->output_open) - return; - - if( buf->decoder_flags & BUF_FLAG_PREVIEW ) - return; - - ff_audio_ensure_buffer_size(this, this->size + buf->size); - xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size); - this->size += buf->size; - - if (buf->decoder_flags & BUF_FLAG_FRAME_END) { /* time to decode a frame */ - - offset = 0; - while (this->size>0) { - decode_buffer_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; - bytes_consumed = avcodec_decode_audio2 (this->context, - (int16_t *)this->decode_buffer, - &decode_buffer_size, - &this->buf[offset], - this->size); - - if (bytes_consumed<0) { - xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, - "ffmpeg_audio_dec: error decompressing audio frame\n"); - this->size=0; - return; - } else if (bytes_consumed == 0 && decode_buffer_size == 0) { - if (offset) - memmove(this->buf, &this->buf[offset], this->size); - return; - } - - /* dispatch the decoded audio */ - out = 0; - while (out < decode_buffer_size) { - audio_buffer = - this->stream->audio_out->get_buffer (this->stream->audio_out); - if (audio_buffer->mem_size == 0) { - xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, - "ffmpeg_audio_dec: Help! Allocated audio buffer with nothing in it!\n"); - return; - } - - if ((decode_buffer_size - out) > audio_buffer->mem_size) - bytes_to_send = audio_buffer->mem_size; - else - bytes_to_send = decode_buffer_size - out; - - /* fill up this buffer */ - xine_fast_memcpy(audio_buffer->mem, &this->decode_buffer[out], - bytes_to_send); - /* byte count / 2 (bytes / sample) / channels */ - audio_buffer->num_frames = bytes_to_send / 2 / this->audio_channels; - - audio_buffer->vpts = buf->pts; - buf->pts = 0; /* only first buffer gets the real pts */ - this->stream->audio_out->put_buffer (this->stream->audio_out, - audio_buffer, this->stream); - - out += bytes_to_send; - } - - this->size -= bytes_consumed; - offset += bytes_consumed; - } - - /* reset internal accumulation buffer */ - this->size = 0; - } - } -} - -static void ff_audio_reset (audio_decoder_t *this_gen) { - ff_audio_decoder_t *this = (ff_audio_decoder_t *) this_gen; - - this->size = 0; - - /* try to reset the wma decoder */ - if( this->context && this->decoder_ok ) { - pthread_mutex_lock (&ffmpeg_lock); - avcodec_close (this->context); - avcodec_open (this->context, this->codec); - pthread_mutex_unlock (&ffmpeg_lock); - } -} - -static void ff_audio_discontinuity (audio_decoder_t *this_gen) { -} - -static void ff_audio_dispose (audio_decoder_t *this_gen) { - - ff_audio_decoder_t *this = (ff_audio_decoder_t *) this_gen; - - if( this->context && this->decoder_ok ) { - pthread_mutex_lock (&ffmpeg_lock); - avcodec_close (this->context); - pthread_mutex_unlock (&ffmpeg_lock); - } - - if (this->output_open) - this->stream->audio_out->close (this->stream->audio_out, this->stream); - this->output_open = 0; - - free(this->buf); - free(this->decode_buffer); - - if(this->context && this->context->extradata) - free(this->context->extradata); - - if(this->context) - free(this->context); - - free (this_gen); -} - -static audio_decoder_t *ff_audio_open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { - - ff_audio_decoder_t *this ; - - this = (ff_audio_decoder_t *) xine_xmalloc (sizeof (ff_audio_decoder_t)); - - this->audio_decoder.decode_data = ff_audio_decode_data; - this->audio_decoder.reset = ff_audio_reset; - this->audio_decoder.discontinuity = ff_audio_discontinuity; - this->audio_decoder.dispose = ff_audio_dispose; - - this->output_open = 0; - this->audio_channels = 0; - this->stream = stream; - this->buf = NULL; - this->size = 0; - this->bufsize = 0; - this->decoder_ok = 0; - - ff_audio_ensure_buffer_size(this, AUDIOBUFSIZE); - - return &this->audio_decoder; -} - -static char *ff_audio_get_identifier (audio_decoder_class_t *this) { - return "ffmpeg audio"; -} - -static char *ff_audio_get_description (audio_decoder_class_t *this) { - return "ffmpeg based audio decoder plugin"; -} - -static void ff_audio_dispose_class (audio_decoder_class_t *this) { - free (this); -} - -void *init_audio_plugin (xine_t *xine, void *data) { - - ff_audio_class_t *this ; - - this = (ff_audio_class_t *) xine_xmalloc (sizeof (ff_audio_class_t)); - - this->decoder_class.open_plugin = ff_audio_open_plugin; - this->decoder_class.get_identifier = ff_audio_get_identifier; - this->decoder_class.get_description = ff_audio_get_description; - this->decoder_class.dispose = ff_audio_dispose_class; - - pthread_once( &once_control, init_once_routine ); - - return this; -} - -static uint32_t supported_audio_types[] = { - #ifdef CONFIG_WMAV1_DECODER - BUF_AUDIO_WMAV1, - #endif - #ifdef CONFIG_WMAV2_DECODER - BUF_AUDIO_WMAV2, - #endif - #ifdef CONFIG_RA_144_DECODER - BUF_AUDIO_14_4, - #endif - #ifdef CONFIG_RA_288_DECODER - BUF_AUDIO_28_8, - #endif - #ifdef CONFIG_MP3_DECODER - BUF_AUDIO_MPEG, - #endif - #ifdef CONFIG_ADPCM_MS_DECODER - BUF_AUDIO_MSADPCM, - #endif - #ifdef CONFIG_ADPCM_IMA_QT_DECODER - BUF_AUDIO_QTIMAADPCM, - #endif - #ifdef CONFIG_ADPCM_IMA_WAV_DECODER - BUF_AUDIO_MSIMAADPCM, - #endif - #ifdef CONFIG_ADPCM_IMA_DK3_DECODER - BUF_AUDIO_DK3ADPCM, - #endif - #ifdef CONFIG_ADPCM_IMA_DK4_DECODER - BUF_AUDIO_DK4ADPCM, - #endif - #ifdef CONFIG_ADPCM_IMA_WS_DECODER - BUF_AUDIO_VQA_IMA, - #endif - #ifdef CONFIG_ADPCM_IMA_SMJPEG_DECODER - BUF_AUDIO_SMJPEG_IMA, - #endif - #ifdef CONFIG_ADPCM_XA_DECODER - BUF_AUDIO_XA_ADPCM, - #endif - #ifdef CONFIG_ADPCM_4XM_DECODER - BUF_AUDIO_4X_ADPCM, - #endif - #ifdef CONFIG_ADPCM_EA_DECODER - BUF_AUDIO_EA_ADPCM, - #endif - #ifdef CONFIG_PCM_MULAW_DECODER - BUF_AUDIO_MULAW, - #endif - #ifdef CONFIG_PCM_ALAW_DECODER - BUF_AUDIO_ALAW, - #endif - #ifdef CONFIG_ROQ_DPCM_DECODER - BUF_AUDIO_ROQ, - #endif - #ifdef CONFIG_INTERPLAY_DPCM_DECODER - BUF_AUDIO_INTERPLAY, - #endif - #ifdef CONFIG_MACE3_DECODER - BUF_AUDIO_MAC3, - #endif - #ifdef CONFIG_MACE6_DECODER - BUF_AUDIO_MAC6, - #endif - #ifdef CONFIG_XAN_DPCM_DECODER - BUF_AUDIO_XAN_DPCM, - #endif - #ifdef CONFIG_VMDAUDIO_DECODER - BUF_AUDIO_VMD, - #endif - #ifdef CONFIG_FLAC_DECODER - BUF_AUDIO_FLAC, - #endif - #ifdef CONFIG_SHORTEN_DECODER - BUF_AUDIO_SHORTEN, - #endif - #ifdef CONFIG_ALAC_DECODER - BUF_AUDIO_ALAC, - #endif - #ifdef CONFIG_QDM2_DECODER - BUF_AUDIO_QDESIGN2, - #endif - #ifdef CONFIG_COOK_DECODER - BUF_AUDIO_COOK, - #endif - #ifdef CONFIG_TRUESPEECH_DECODER - BUF_AUDIO_TRUESPEECH, - #endif - #ifdef CONFIG_TTA_DECODER - BUF_AUDIO_TTA, - #endif - #ifdef CONFIG_SMACKAUDIO_DECODER - BUF_AUDIO_SMACKER, - #endif - #ifdef CONFIG_ADPCM_SWF_DECODER - BUF_AUDIO_FLVADPCM, - #endif - #ifdef CONFIG_WAVPACK_DECODER - BUF_AUDIO_WAVPACK, - #endif - - 0 -}; - -decoder_info_t dec_info_ffmpeg_audio = { - supported_audio_types, /* supported types */ - 6 /* priority */ -}; diff --git a/src/libffmpeg/dvaudio_decoder.c b/src/libffmpeg/dvaudio_decoder.c deleted file mode 100644 index 2c851bae1..000000000 --- a/src/libffmpeg/dvaudio_decoder.c +++ /dev/null @@ -1,433 +0,0 @@ -/* - * Copyright (C) 2005 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: dvaudio_decoder.c,v 1.12 2006/12/04 22:25:13 miguelfreitas Exp $ - * - * dv audio decoder based on patch by Dan Dennedy - * - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include -#include -#include -#include - -#define LOG_MODULE "dvaudio" -#define LOG_VERBOSE -/* -#define LOG -*/ - -#include "xine_internal.h" -#include "buffer.h" -#include "xineutils.h" - -#ifdef _MSC_VER -/* ffmpeg has own definitions of those types */ -# undef int8_t -# undef uint8_t -# undef int16_t -# undef uint16_t -# undef int32_t -# undef uint32_t -# undef int64_t -# undef uint64_t -#endif - -#ifdef HAVE_FFMPEG -# include -# include "libavcodec/dvdata.h" -#else -# include "libavcodec/avcodec.h" -# include "libavcodec/dvdata.h" -#endif - -#ifdef _MSC_VER -# undef malloc -# undef free -# undef realloc -#endif - -#define AUDIOBUFSIZE 128*1024 -#define MAXFRAMESIZE 131072 - - -typedef struct { - audio_decoder_class_t decoder_class; -} dvaudio_class_t; - -typedef struct dvaudio_decoder_s { - audio_decoder_t audio_decoder; - - xine_stream_t *stream; - - int output_open; - int audio_channels; - int audio_bits; - int audio_sample_rate; - - unsigned char *buf; - int bufsize; - int size; - - char *decode_buffer; - int decoder_ok; - -} dvaudio_decoder_t; - - -/* - * This is the dumbest implementation of all -- it simply looks at - * a fixed offset and if pack isn't there -- fails. We might want - * to have a fallback mechanism for complete search of missing packs. - */ -static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t) -{ - int offs; - - switch (t) { - case dv_audio_source: - offs = (80*6 + 80*16*3 + 3); - break; - case dv_audio_control: - offs = (80*6 + 80*16*4 + 3); - break; - case dv_video_control: - offs = (80*5 + 48 + 5); - break; - default: - return NULL; - } - - return (frame[offs] == t ? &frame[offs] : NULL); -} - -static inline uint16_t dv_audio_12to16(uint16_t sample) -{ - uint16_t shift, result; - - sample = (sample < 0x800) ? sample : sample | 0xf000; - shift = (sample & 0xf00) >> 8; - - if (shift < 0x2 || shift > 0xd) { - result = sample; - } else if (shift < 0x8) { - shift--; - result = (sample - (256 * shift)) << shift; - } else { - shift = 0xe - shift; - result = ((sample + ((256 * shift) + 1)) << shift) - 1; - } - - return result; -} - -/* - * There's a couple of assumptions being made here: - * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples. - * We can pass them upwards when ffmpeg will be ready to deal with them. - * 2. We don't do software emphasis. - * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples - * are converted into 16bit linear ones. - */ -static int dv_extract_audio(uint8_t* frame, uint8_t* pcm, uint8_t* pcm2) -{ - int size, i, j, d, of, smpls, freq, quant, half_ch; - uint16_t lc, rc; - const DVprofile* sys; - const uint8_t* as_pack; - - as_pack = dv_extract_pack(frame, dv_audio_source); - if (!as_pack) /* No audio ? */ - return 0; - - sys = dv_frame_profile(frame); - smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */ - freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */ - quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */ - - if (quant > 1) - return -1; /* Unsupported quantization */ - - size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */ - half_ch = sys->difseg_size/2; - - /* for each DIF segment */ - for (i = 0; i < sys->difseg_size; i++) { - frame += 6 * 80; /* skip DIF segment header */ - if (quant == 1 && i == half_ch) { - if (!pcm2) - break; - else - pcm = pcm2; - } - - for (j = 0; j < 9; j++) { - for (d = 8; d < 80; d += 2) { - if (quant == 0) { /* 16bit quantization */ - of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride; - if (of*2 >= size) - continue; - -#ifdef WORDS_BIGENDIAN - pcm[of*2] = frame[d]; - pcm[of*2+1] = frame[d+1]; -#else - pcm[of*2] = frame[d+1]; - pcm[of*2+1] = frame[d]; -#endif - if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00) - pcm[of*2+1] = 0; - } else { /* 12bit quantization */ - lc = ((uint16_t)frame[d] << 4) | - ((uint16_t)frame[d+2] >> 4); - rc = ((uint16_t)frame[d+1] << 4) | - ((uint16_t)frame[d+2] & 0x0f); - lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc)); - rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc)); - - of = sys->audio_shuffle[i%half_ch][j] + (d - 8)/3 * sys->audio_stride; - if (of*2 >= size) - continue; - -#ifdef WORDS_BIGENDIAN - pcm[of*2] = lc >> 8; - pcm[of*2+1] = lc & 0xff; -#else - pcm[of*2] = lc & 0xff; - pcm[of*2+1] = lc >> 8; -#endif - of = sys->audio_shuffle[i%half_ch+half_ch][j] + - (d - 8)/3 * sys->audio_stride; -#ifdef WORDS_BIGENDIAN - pcm[of*2] = rc >> 8; - pcm[of*2+1] = rc & 0xff; -#else - pcm[of*2] = rc & 0xff; - pcm[of*2+1] = rc >> 8; -#endif - ++d; - } - } - - frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ - } - } - - return size; -} - -static void dvaudio_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { - - dvaudio_decoder_t *this = (dvaudio_decoder_t *) this_gen; - int bytes_consumed; - int decode_buffer_size; - int offset; - int out; - audio_buffer_t *audio_buffer; - int bytes_to_send; - - if (buf->decoder_flags & BUF_FLAG_PREVIEW) - return; - - if (buf->decoder_flags & BUF_FLAG_STDHEADER) { - this->buf = xine_xmalloc(AUDIOBUFSIZE); - this->bufsize = AUDIOBUFSIZE; - this->size = 0; - this->decode_buffer = xine_xmalloc(MAXFRAMESIZE); - - this->audio_sample_rate = buf->decoder_info[1]; - this->audio_bits = buf->decoder_info[2]; - this->audio_channels = buf->decoder_info[3]; - - _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "DV Audio"); - - this->decoder_ok = 1; - - return; - } - - if (this->decoder_ok && !(buf->decoder_flags & (BUF_FLAG_HEADER|BUF_FLAG_SPECIAL))) { - - if (!this->output_open) { - this->output_open = this->stream->audio_out->open(this->stream->audio_out, - this->stream, this->audio_bits, this->audio_sample_rate, - _x_ao_channels2mode(this->audio_channels)); - } - - /* if the audio still isn't open, bail */ - if (!this->output_open) - return; - - if( this->size + buf->size > this->bufsize ) { - this->bufsize = this->size + 2 * buf->size; - xprintf(this->stream->xine, XINE_VERBOSITY_LOG, - _("dvaudio: increasing 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_FRAME_END) { /* time to decode a frame */ - - offset = 0; - while (this->size>0) { - decode_buffer_size = dv_extract_audio(&this->buf[offset], this->decode_buffer, NULL); - - if (decode_buffer_size > -1) - bytes_consumed = dv_frame_profile(&this->buf[offset])->frame_size; - else - bytes_consumed = decode_buffer_size; - - /* dispatch the decoded audio */ - out = 0; - while (out < decode_buffer_size) { - audio_buffer = - this->stream->audio_out->get_buffer (this->stream->audio_out); - if (audio_buffer->mem_size == 0) { - xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, - "dvaudio: Help! Allocated audio buffer with nothing in it!\n"); - return; - } - - if ((decode_buffer_size - out) > audio_buffer->mem_size) - bytes_to_send = audio_buffer->mem_size; - else - bytes_to_send = decode_buffer_size - out; - - /* fill up this buffer */ - xine_fast_memcpy(audio_buffer->mem, &this->decode_buffer[out], - bytes_to_send); - /* byte count / 2 (bytes / sample) / channels */ - audio_buffer->num_frames = bytes_to_send / 2 / this->audio_channels; - - audio_buffer->vpts = buf->pts; - buf->pts = 0; /* only first buffer gets the real pts */ - this->stream->audio_out->put_buffer (this->stream->audio_out, - audio_buffer, this->stream); - - out += bytes_to_send; - } - - this->size -= bytes_consumed; - offset += bytes_consumed; - } - - /* reset internal accumulation buffer */ - this->size = 0; - } - } -} - -static void dvaudio_reset (audio_decoder_t *this_gen) { - dvaudio_decoder_t *this = (dvaudio_decoder_t *) this_gen; - - this->size = 0; -} - -static void dvaudio_discontinuity (audio_decoder_t *this_gen) { -} - -static void dvaudio_dispose (audio_decoder_t *this_gen) { - - dvaudio_decoder_t *this = (dvaudio_decoder_t *) this_gen; - - if (this->output_open) - this->stream->audio_out->close (this->stream->audio_out, this->stream); - this->output_open = 0; - - free(this->buf); - free(this->decode_buffer); - - free (this_gen); -} - -static audio_decoder_t *dvaudio_open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { - - dvaudio_decoder_t *this ; - - this = (dvaudio_decoder_t *) xine_xmalloc (sizeof (dvaudio_decoder_t)); - - this->audio_decoder.decode_data = dvaudio_decode_data; - this->audio_decoder.reset = dvaudio_reset; - this->audio_decoder.discontinuity = dvaudio_discontinuity; - this->audio_decoder.dispose = dvaudio_dispose; - - this->output_open = 0; - this->audio_channels = 0; - this->stream = stream; - this->buf = NULL; - this->size = 0; - this->decoder_ok = 0; - - return &this->audio_decoder; -} - -static char *dvaudio_get_identifier (audio_decoder_class_t *this) { - return "dv audio"; -} - -static char *dvaudio_get_description (audio_decoder_class_t *this) { - return "dv audio decoder plugin"; -} - -static void dvaudio_dispose_class (audio_decoder_class_t *this) { - free (this); -} - -static void *init_dvaudio_plugin (xine_t *xine, void *data) { - - dvaudio_class_t *this ; - - this = (dvaudio_class_t *) xine_xmalloc (sizeof (dvaudio_class_t)); - - this->decoder_class.open_plugin = dvaudio_open_plugin; - this->decoder_class.get_identifier = dvaudio_get_identifier; - this->decoder_class.get_description = dvaudio_get_description; - this->decoder_class.dispose = dvaudio_dispose_class; - - return this; -} - -static uint32_t supported_audio_types[] = { - BUF_AUDIO_DV, - 0 -}; - -static const decoder_info_t dec_info_dvaudio = { - supported_audio_types, /* supported types */ - 5 /* priority */ -}; - -/* - * exported plugin catalog entry - */ - -const plugin_info_t xine_plugin_info[] EXPORTED = { - /* type, API, "name", version, special_info, init_function */ - { PLUGIN_AUDIO_DECODER, 15, "dvaudio", XINE_VERSION_CODE, &dec_info_dvaudio, init_dvaudio_plugin }, - { PLUGIN_NONE, 0, "", 0, NULL, NULL } -}; diff --git a/src/libffmpeg/ff_audio_decoder.c b/src/libffmpeg/ff_audio_decoder.c new file mode 100644 index 000000000..e0d6e5cab --- /dev/null +++ b/src/libffmpeg/ff_audio_decoder.c @@ -0,0 +1,554 @@ +/* + * Copyright (C) 2001-2005 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: audio_decoder.c,v 1.33 2007/01/28 18:38:33 miguelfreitas Exp $ + * + * xine audio decoder plugin using ffmpeg + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#include "ffmpeg_config.h" +#endif + +#include +#include +#include +#include +#include +#include + +#define LOG_MODULE "ffmpeg_audio_dec" +#define LOG_VERBOSE +/* +#define LOG +*/ + +#include "xine_internal.h" +#include "buffer.h" +#include "xineutils.h" +#include "bswap.h" +#include "ffmpeg_decoder.h" + +#define AUDIOBUFSIZE (64 * 1024) + +typedef struct { + audio_decoder_class_t decoder_class; +} ff_audio_class_t; + +typedef struct ff_audio_decoder_s { + audio_decoder_t audio_decoder; + + xine_stream_t *stream; + + int output_open; + int audio_channels; + int audio_bits; + int audio_sample_rate; + + unsigned char *buf; + int bufsize; + int size; + + AVCodecContext *context; + AVCodec *codec; + + char *decode_buffer; + int decoder_ok; + +} ff_audio_decoder_t; + + +static const ff_codec_t ff_audio_lookup[] = { + {BUF_AUDIO_WMAV1, CODEC_ID_WMAV1, "MS Windows Media Audio 1 (ffmpeg)"}, + {BUF_AUDIO_WMAV2, CODEC_ID_WMAV2, "MS Windows Media Audio 2 (ffmpeg)"}, + {BUF_AUDIO_14_4, CODEC_ID_RA_144, "Real 14.4 (ffmpeg)"}, + {BUF_AUDIO_28_8, CODEC_ID_RA_288, "Real 28.8 (ffmpeg)"}, + {BUF_AUDIO_MPEG, CODEC_ID_MP3, "MP3 (ffmpeg)"}, + {BUF_AUDIO_MSADPCM, CODEC_ID_ADPCM_MS, "MS ADPCM (ffmpeg)"}, + {BUF_AUDIO_QTIMAADPCM, CODEC_ID_ADPCM_IMA_QT, "QT IMA ADPCM (ffmpeg)"}, + {BUF_AUDIO_MSIMAADPCM, CODEC_ID_ADPCM_IMA_WAV, "MS IMA ADPCM (ffmpeg)"}, + {BUF_AUDIO_DK3ADPCM, CODEC_ID_ADPCM_IMA_DK3, "Duck DK3 ADPCM (ffmpeg)"}, + {BUF_AUDIO_DK4ADPCM, CODEC_ID_ADPCM_IMA_DK4, "Duck DK4 ADPCM (ffmpeg)"}, + {BUF_AUDIO_VQA_IMA, CODEC_ID_ADPCM_IMA_WS, "Westwood Studios IMA (ffmpeg)"}, + {BUF_AUDIO_SMJPEG_IMA, CODEC_ID_ADPCM_IMA_SMJPEG, "SMJPEG IMA (ffmpeg)"}, + {BUF_AUDIO_XA_ADPCM, CODEC_ID_ADPCM_XA, "CD-ROM/XA ADPCM (ffmpeg)"}, + {BUF_AUDIO_4X_ADPCM, CODEC_ID_ADPCM_4XM, "4X ADPCM (ffmpeg)"}, + {BUF_AUDIO_EA_ADPCM, CODEC_ID_ADPCM_EA, "Electronic Arts ADPCM (ffmpeg)"}, + {BUF_AUDIO_MULAW, CODEC_ID_PCM_MULAW, "mu-law logarithmic PCM (ffmpeg)"}, + {BUF_AUDIO_ALAW, CODEC_ID_PCM_ALAW, "A-law logarithmic PCM (ffmpeg)"}, + {BUF_AUDIO_ROQ, CODEC_ID_ROQ_DPCM, "RoQ DPCM (ffmpeg)"}, + {BUF_AUDIO_INTERPLAY, CODEC_ID_INTERPLAY_DPCM, "Interplay DPCM (ffmpeg)"}, + {BUF_AUDIO_MAC3, CODEC_ID_MACE3, "MACE 3:1 (ffmpeg)"}, + {BUF_AUDIO_MAC6, CODEC_ID_MACE6, "MACE 6:1 (ffmpeg)"}, + {BUF_AUDIO_XAN_DPCM, CODEC_ID_XAN_DPCM, "Origin Xan DPCM (ffmpeg)"}, + {BUF_AUDIO_VMD, CODEC_ID_VMDAUDIO, "Sierra VMD Audio (ffmpeg)"}, + {BUF_AUDIO_FLAC, CODEC_ID_FLAC, "FLAC (ffmpeg)"}, + {BUF_AUDIO_SHORTEN, CODEC_ID_SHORTEN, "Shorten (ffmpeg)"}, + {BUF_AUDIO_ALAC, CODEC_ID_ALAC, "ALAC (ffmpeg)"}, + {BUF_AUDIO_QDESIGN2, CODEC_ID_QDM2, "QDesign (ffmpeg)"}, + {BUF_AUDIO_COOK, CODEC_ID_COOK, "RealAudio Cooker (ffmpeg)"}, + {BUF_AUDIO_TRUESPEECH, CODEC_ID_TRUESPEECH, "TrueSpeech (ffmpeg)"}, + {BUF_AUDIO_TTA, CODEC_ID_TTA, "True Audio Lossless (ffmpeg)"}, + {BUF_AUDIO_SMACKER, CODEC_ID_SMACKAUDIO, "Smacker (ffmpeg)"}, + {BUF_AUDIO_FLVADPCM, CODEC_ID_ADPCM_SWF, "Flash ADPCM (ffmpeg)"}, + {BUF_AUDIO_WAVPACK, CODEC_ID_WAVPACK, "WavPack (ffmpeg)"}, +}; + + + static void ff_audio_ensure_buffer_size(ff_audio_decoder_t *this, int size) { + if (size > this->bufsize) { + this->bufsize = size + size / 2; + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + _("ffmpeg_audio_dec: increasing buffer to %d to avoid overflow.\n"), + this->bufsize); + this->buf = realloc( this->buf, this->bufsize ); + } +} + +static void ff_audio_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { + + ff_audio_decoder_t *this = (ff_audio_decoder_t *) this_gen; + int bytes_consumed; + int decode_buffer_size; + int offset; + int out; + audio_buffer_t *audio_buffer; + int bytes_to_send; + + if ( (buf->decoder_flags & BUF_FLAG_HEADER) && + !(buf->decoder_flags & BUF_FLAG_SPECIAL) ) { + + /* accumulate init data */ + ff_audio_ensure_buffer_size(this, this->size + buf->size); + memcpy(this->buf + this->size, buf->content, buf->size); + this->size += buf->size; + + if (buf->decoder_flags & BUF_FLAG_FRAME_END) { + size_t i; + unsigned int codec_type; + xine_waveformatex *audio_header; + + codec_type = buf->type & 0xFFFF0000; + this->codec = NULL; + + for(i = 0; i < sizeof(ff_audio_lookup)/sizeof(ff_codec_t); i++) + if(ff_audio_lookup[i].type == codec_type) { + pthread_mutex_lock (&ffmpeg_lock); + this->codec = avcodec_find_decoder(ff_audio_lookup[i].id); + pthread_mutex_unlock (&ffmpeg_lock); + _x_meta_info_set(this->stream, XINE_META_INFO_AUDIOCODEC, + ff_audio_lookup[i].name); + break; + } + + if (!this->codec) { + xprintf (this->stream->xine, XINE_VERBOSITY_LOG, + _("ffmpeg_audio_dec: couldn't find ffmpeg decoder for buf type 0x%X\n"), + codec_type); + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_HANDLED, 0); + return; + } + + this->context = avcodec_alloc_context(); + + if(buf->decoder_flags & BUF_FLAG_STDHEADER) { + this->audio_sample_rate = buf->decoder_info[1]; + this->audio_channels = buf->decoder_info[3]; + + if(this->size) { + audio_header = (xine_waveformatex *)this->buf; + + this->context->block_align = audio_header->nBlockAlign; + this->context->bit_rate = audio_header->nAvgBytesPerSec * 8; + + if(audio_header->cbSize > 0) { + this->context->extradata = xine_xmalloc(audio_header->cbSize); + this->context->extradata_size = audio_header->cbSize; + memcpy( this->context->extradata, + (uint8_t *)audio_header + sizeof(xine_waveformatex), + audio_header->cbSize ); + } + } + } else { + short *ptr; + + switch(codec_type) { + case BUF_AUDIO_14_4: + this->audio_sample_rate = 8000; + this->audio_channels = 1; + + this->context->block_align = 240; + break; + case BUF_AUDIO_28_8: + this->audio_sample_rate = BE_16(&this->buf[0x30]); + this->audio_channels = this->buf[0x37]; + /* this->audio_bits = buf->content[0x35] */ + + this->context->block_align = BE_16(&this->buf[0x2A]); + + this->context->extradata_size = 5*sizeof(short); + this->context->extradata = xine_xmalloc(this->context->extradata_size); + + ptr = (short *) this->context->extradata; + + ptr[0] = BE_16(&this->buf[0x2C]); /* subpacket size */ + ptr[1] = BE_16(&this->buf[0x28]); /* subpacket height */ + ptr[2] = BE_16(&this->buf[0x16]); /* subpacket flavour */ + ptr[3] = BE_32(&this->buf[0x18]); /* coded frame size */ + ptr[4] = 0; /* codec's data length */ + break; + default: + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + "ffmpeg_audio_dec: unknown header with buf type 0x%X\n", codec_type); + break; + } + } + + /* Current ffmpeg audio decoders always use 16 bits/sample + * buf->decoder_info[2] can't be used as it doesn't refer to the output + * bits/sample for some codecs (e.g. MS ADPCM) */ + this->audio_bits = 16; + + this->context->bits_per_sample = this->audio_bits; + this->context->sample_rate = this->audio_sample_rate; + this->context->channels = this->audio_channels; + this->context->codec_id = this->codec->id; + this->context->codec_tag = _x_stream_info_get(this->stream, XINE_STREAM_INFO_AUDIO_FOURCC); + + this->size = 0; + + this->decode_buffer = xine_xmalloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); + + return; + } + } else if ((buf->decoder_flags & BUF_FLAG_SPECIAL) && + (buf->decoder_info[1] == BUF_SPECIAL_STSD_ATOM)) { + + this->context->extradata_size = buf->decoder_info[2]; + this->context->extradata = xine_xmalloc(buf->decoder_info[2] + + FF_INPUT_BUFFER_PADDING_SIZE); + memcpy(this->context->extradata, buf->decoder_info_ptr[2], + buf->decoder_info[2]); + + } else if (!(buf->decoder_flags & BUF_FLAG_SPECIAL)) { + + if( !this->decoder_ok ) { + if ( ! this->context || ! this->codec ) { + xprintf (this->stream->xine, XINE_VERBOSITY_LOG, + _("ffmpeg_audio_dec: trying to open null codec\n")); + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_HANDLED, 0); + return; + } + + pthread_mutex_lock (&ffmpeg_lock); + if (avcodec_open (this->context, this->codec) < 0) { + pthread_mutex_unlock (&ffmpeg_lock); + xprintf (this->stream->xine, XINE_VERBOSITY_LOG, + _("ffmpeg_audio_dec: couldn't open decoder\n")); + _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_HANDLED, 0); + return; + } + pthread_mutex_unlock (&ffmpeg_lock); + this->decoder_ok = 1; + } + + if (!this->output_open) { + this->output_open = this->stream->audio_out->open(this->stream->audio_out, + this->stream, this->audio_bits, this->audio_sample_rate, + _x_ao_channels2mode(this->audio_channels)); + } + + /* if the audio still isn't open, bail */ + if (!this->output_open) + return; + + if( buf->decoder_flags & BUF_FLAG_PREVIEW ) + return; + + ff_audio_ensure_buffer_size(this, this->size + buf->size); + xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size); + this->size += buf->size; + + if (buf->decoder_flags & BUF_FLAG_FRAME_END) { /* time to decode a frame */ + + offset = 0; + while (this->size>0) { + decode_buffer_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; + bytes_consumed = avcodec_decode_audio2 (this->context, + (int16_t *)this->decode_buffer, + &decode_buffer_size, + &this->buf[offset], + this->size); + + if (bytes_consumed<0) { + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, + "ffmpeg_audio_dec: error decompressing audio frame\n"); + this->size=0; + return; + } else if (bytes_consumed == 0 && decode_buffer_size == 0) { + if (offset) + memmove(this->buf, &this->buf[offset], this->size); + return; + } + + /* dispatch the decoded audio */ + out = 0; + while (out < decode_buffer_size) { + audio_buffer = + this->stream->audio_out->get_buffer (this->stream->audio_out); + if (audio_buffer->mem_size == 0) { + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, + "ffmpeg_audio_dec: Help! Allocated audio buffer with nothing in it!\n"); + return; + } + + if ((decode_buffer_size - out) > audio_buffer->mem_size) + bytes_to_send = audio_buffer->mem_size; + else + bytes_to_send = decode_buffer_size - out; + + /* fill up this buffer */ + xine_fast_memcpy(audio_buffer->mem, &this->decode_buffer[out], + bytes_to_send); + /* byte count / 2 (bytes / sample) / channels */ + audio_buffer->num_frames = bytes_to_send / 2 / this->audio_channels; + + audio_buffer->vpts = buf->pts; + buf->pts = 0; /* only first buffer gets the real pts */ + this->stream->audio_out->put_buffer (this->stream->audio_out, + audio_buffer, this->stream); + + out += bytes_to_send; + } + + this->size -= bytes_consumed; + offset += bytes_consumed; + } + + /* reset internal accumulation buffer */ + this->size = 0; + } + } +} + +static void ff_audio_reset (audio_decoder_t *this_gen) { + ff_audio_decoder_t *this = (ff_audio_decoder_t *) this_gen; + + this->size = 0; + + /* try to reset the wma decoder */ + if( this->context && this->decoder_ok ) { + pthread_mutex_lock (&ffmpeg_lock); + avcodec_close (this->context); + avcodec_open (this->context, this->codec); + pthread_mutex_unlock (&ffmpeg_lock); + } +} + +static void ff_audio_discontinuity (audio_decoder_t *this_gen) { +} + +static void ff_audio_dispose (audio_decoder_t *this_gen) { + + ff_audio_decoder_t *this = (ff_audio_decoder_t *) this_gen; + + if( this->context && this->decoder_ok ) { + pthread_mutex_lock (&ffmpeg_lock); + avcodec_close (this->context); + pthread_mutex_unlock (&ffmpeg_lock); + } + + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + this->output_open = 0; + + free(this->buf); + free(this->decode_buffer); + + if(this->context && this->context->extradata) + free(this->context->extradata); + + if(this->context) + free(this->context); + + free (this_gen); +} + +static audio_decoder_t *ff_audio_open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { + + ff_audio_decoder_t *this ; + + this = (ff_audio_decoder_t *) xine_xmalloc (sizeof (ff_audio_decoder_t)); + + this->audio_decoder.decode_data = ff_audio_decode_data; + this->audio_decoder.reset = ff_audio_reset; + this->audio_decoder.discontinuity = ff_audio_discontinuity; + this->audio_decoder.dispose = ff_audio_dispose; + + this->output_open = 0; + this->audio_channels = 0; + this->stream = stream; + this->buf = NULL; + this->size = 0; + this->bufsize = 0; + this->decoder_ok = 0; + + ff_audio_ensure_buffer_size(this, AUDIOBUFSIZE); + + return &this->audio_decoder; +} + +static char *ff_audio_get_identifier (audio_decoder_class_t *this) { + return "ffmpeg audio"; +} + +static char *ff_audio_get_description (audio_decoder_class_t *this) { + return "ffmpeg based audio decoder plugin"; +} + +static void ff_audio_dispose_class (audio_decoder_class_t *this) { + free (this); +} + +void *init_audio_plugin (xine_t *xine, void *data) { + + ff_audio_class_t *this ; + + this = (ff_audio_class_t *) xine_xmalloc (sizeof (ff_audio_class_t)); + + this->decoder_class.open_plugin = ff_audio_open_plugin; + this->decoder_class.get_identifier = ff_audio_get_identifier; + this->decoder_class.get_description = ff_audio_get_description; + this->decoder_class.dispose = ff_audio_dispose_class; + + pthread_once( &once_control, init_once_routine ); + + return this; +} + +static uint32_t supported_audio_types[] = { + #ifdef CONFIG_WMAV1_DECODER + BUF_AUDIO_WMAV1, + #endif + #ifdef CONFIG_WMAV2_DECODER + BUF_AUDIO_WMAV2, + #endif + #ifdef CONFIG_RA_144_DECODER + BUF_AUDIO_14_4, + #endif + #ifdef CONFIG_RA_288_DECODER + BUF_AUDIO_28_8, + #endif + #ifdef CONFIG_MP3_DECODER + BUF_AUDIO_MPEG, + #endif + #ifdef CONFIG_ADPCM_MS_DECODER + BUF_AUDIO_MSADPCM, + #endif + #ifdef CONFIG_ADPCM_IMA_QT_DECODER + BUF_AUDIO_QTIMAADPCM, + #endif + #ifdef CONFIG_ADPCM_IMA_WAV_DECODER + BUF_AUDIO_MSIMAADPCM, + #endif + #ifdef CONFIG_ADPCM_IMA_DK3_DECODER + BUF_AUDIO_DK3ADPCM, + #endif + #ifdef CONFIG_ADPCM_IMA_DK4_DECODER + BUF_AUDIO_DK4ADPCM, + #endif + #ifdef CONFIG_ADPCM_IMA_WS_DECODER + BUF_AUDIO_VQA_IMA, + #endif + #ifdef CONFIG_ADPCM_IMA_SMJPEG_DECODER + BUF_AUDIO_SMJPEG_IMA, + #endif + #ifdef CONFIG_ADPCM_XA_DECODER + BUF_AUDIO_XA_ADPCM, + #endif + #ifdef CONFIG_ADPCM_4XM_DECODER + BUF_AUDIO_4X_ADPCM, + #endif + #ifdef CONFIG_ADPCM_EA_DECODER + BUF_AUDIO_EA_ADPCM, + #endif + #ifdef CONFIG_PCM_MULAW_DECODER + BUF_AUDIO_MULAW, + #endif + #ifdef CONFIG_PCM_ALAW_DECODER + BUF_AUDIO_ALAW, + #endif + #ifdef CONFIG_ROQ_DPCM_DECODER + BUF_AUDIO_ROQ, + #endif + #ifdef CONFIG_INTERPLAY_DPCM_DECODER + BUF_AUDIO_INTERPLAY, + #endif + #ifdef CONFIG_MACE3_DECODER + BUF_AUDIO_MAC3, + #endif + #ifdef CONFIG_MACE6_DECODER + BUF_AUDIO_MAC6, + #endif + #ifdef CONFIG_XAN_DPCM_DECODER + BUF_AUDIO_XAN_DPCM, + #endif + #ifdef CONFIG_VMDAUDIO_DECODER + BUF_AUDIO_VMD, + #endif + #ifdef CONFIG_FLAC_DECODER + BUF_AUDIO_FLAC, + #endif + #ifdef CONFIG_SHORTEN_DECODER + BUF_AUDIO_SHORTEN, + #endif + #ifdef CONFIG_ALAC_DECODER + BUF_AUDIO_ALAC, + #endif + #ifdef CONFIG_QDM2_DECODER + BUF_AUDIO_QDESIGN2, + #endif + #ifdef CONFIG_COOK_DECODER + BUF_AUDIO_COOK, + #endif + #ifdef CONFIG_TRUESPEECH_DECODER + BUF_AUDIO_TRUESPEECH, + #endif + #ifdef CONFIG_TTA_DECODER + BUF_AUDIO_TTA, + #endif + #ifdef CONFIG_SMACKAUDIO_DECODER + BUF_AUDIO_SMACKER, + #endif + #ifdef CONFIG_ADPCM_SWF_DECODER + BUF_AUDIO_FLVADPCM, + #endif + #ifdef CONFIG_WAVPACK_DECODER + BUF_AUDIO_WAVPACK, + #endif + + 0 +}; + +decoder_info_t dec_info_ffmpeg_audio = { + supported_audio_types, /* supported types */ + 6 /* priority */ +}; diff --git a/src/libffmpeg/ff_dvaudio_decoder.c b/src/libffmpeg/ff_dvaudio_decoder.c new file mode 100644 index 000000000..2c851bae1 --- /dev/null +++ b/src/libffmpeg/ff_dvaudio_decoder.c @@ -0,0 +1,433 @@ +/* + * Copyright (C) 2005 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: dvaudio_decoder.c,v 1.12 2006/12/04 22:25:13 miguelfreitas Exp $ + * + * dv audio decoder based on patch by Dan Dennedy + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include + +#define LOG_MODULE "dvaudio" +#define LOG_VERBOSE +/* +#define LOG +*/ + +#include "xine_internal.h" +#include "buffer.h" +#include "xineutils.h" + +#ifdef _MSC_VER +/* ffmpeg has own definitions of those types */ +# undef int8_t +# undef uint8_t +# undef int16_t +# undef uint16_t +# undef int32_t +# undef uint32_t +# undef int64_t +# undef uint64_t +#endif + +#ifdef HAVE_FFMPEG +# include +# include "libavcodec/dvdata.h" +#else +# include "libavcodec/avcodec.h" +# include "libavcodec/dvdata.h" +#endif + +#ifdef _MSC_VER +# undef malloc +# undef free +# undef realloc +#endif + +#define AUDIOBUFSIZE 128*1024 +#define MAXFRAMESIZE 131072 + + +typedef struct { + audio_decoder_class_t decoder_class; +} dvaudio_class_t; + +typedef struct dvaudio_decoder_s { + audio_decoder_t audio_decoder; + + xine_stream_t *stream; + + int output_open; + int audio_channels; + int audio_bits; + int audio_sample_rate; + + unsigned char *buf; + int bufsize; + int size; + + char *decode_buffer; + int decoder_ok; + +} dvaudio_decoder_t; + + +/* + * This is the dumbest implementation of all -- it simply looks at + * a fixed offset and if pack isn't there -- fails. We might want + * to have a fallback mechanism for complete search of missing packs. + */ +static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t) +{ + int offs; + + switch (t) { + case dv_audio_source: + offs = (80*6 + 80*16*3 + 3); + break; + case dv_audio_control: + offs = (80*6 + 80*16*4 + 3); + break; + case dv_video_control: + offs = (80*5 + 48 + 5); + break; + default: + return NULL; + } + + return (frame[offs] == t ? &frame[offs] : NULL); +} + +static inline uint16_t dv_audio_12to16(uint16_t sample) +{ + uint16_t shift, result; + + sample = (sample < 0x800) ? sample : sample | 0xf000; + shift = (sample & 0xf00) >> 8; + + if (shift < 0x2 || shift > 0xd) { + result = sample; + } else if (shift < 0x8) { + shift--; + result = (sample - (256 * shift)) << shift; + } else { + shift = 0xe - shift; + result = ((sample + ((256 * shift) + 1)) << shift) - 1; + } + + return result; +} + +/* + * There's a couple of assumptions being made here: + * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples. + * We can pass them upwards when ffmpeg will be ready to deal with them. + * 2. We don't do software emphasis. + * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples + * are converted into 16bit linear ones. + */ +static int dv_extract_audio(uint8_t* frame, uint8_t* pcm, uint8_t* pcm2) +{ + int size, i, j, d, of, smpls, freq, quant, half_ch; + uint16_t lc, rc; + const DVprofile* sys; + const uint8_t* as_pack; + + as_pack = dv_extract_pack(frame, dv_audio_source); + if (!as_pack) /* No audio ? */ + return 0; + + sys = dv_frame_profile(frame); + smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */ + freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */ + quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */ + + if (quant > 1) + return -1; /* Unsupported quantization */ + + size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */ + half_ch = sys->difseg_size/2; + + /* for each DIF segment */ + for (i = 0; i < sys->difseg_size; i++) { + frame += 6 * 80; /* skip DIF segment header */ + if (quant == 1 && i == half_ch) { + if (!pcm2) + break; + else + pcm = pcm2; + } + + for (j = 0; j < 9; j++) { + for (d = 8; d < 80; d += 2) { + if (quant == 0) { /* 16bit quantization */ + of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride; + if (of*2 >= size) + continue; + +#ifdef WORDS_BIGENDIAN + pcm[of*2] = frame[d]; + pcm[of*2+1] = frame[d+1]; +#else + pcm[of*2] = frame[d+1]; + pcm[of*2+1] = frame[d]; +#endif + if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00) + pcm[of*2+1] = 0; + } else { /* 12bit quantization */ + lc = ((uint16_t)frame[d] << 4) | + ((uint16_t)frame[d+2] >> 4); + rc = ((uint16_t)frame[d+1] << 4) | + ((uint16_t)frame[d+2] & 0x0f); + lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc)); + rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc)); + + of = sys->audio_shuffle[i%half_ch][j] + (d - 8)/3 * sys->audio_stride; + if (of*2 >= size) + continue; + +#ifdef WORDS_BIGENDIAN + pcm[of*2] = lc >> 8; + pcm[of*2+1] = lc & 0xff; +#else + pcm[of*2] = lc & 0xff; + pcm[of*2+1] = lc >> 8; +#endif + of = sys->audio_shuffle[i%half_ch+half_ch][j] + + (d - 8)/3 * sys->audio_stride; +#ifdef WORDS_BIGENDIAN + pcm[of*2] = rc >> 8; + pcm[of*2+1] = rc & 0xff; +#else + pcm[of*2] = rc & 0xff; + pcm[of*2+1] = rc >> 8; +#endif + ++d; + } + } + + frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ + } + } + + return size; +} + +static void dvaudio_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) { + + dvaudio_decoder_t *this = (dvaudio_decoder_t *) this_gen; + int bytes_consumed; + int decode_buffer_size; + int offset; + int out; + audio_buffer_t *audio_buffer; + int bytes_to_send; + + if (buf->decoder_flags & BUF_FLAG_PREVIEW) + return; + + if (buf->decoder_flags & BUF_FLAG_STDHEADER) { + this->buf = xine_xmalloc(AUDIOBUFSIZE); + this->bufsize = AUDIOBUFSIZE; + this->size = 0; + this->decode_buffer = xine_xmalloc(MAXFRAMESIZE); + + this->audio_sample_rate = buf->decoder_info[1]; + this->audio_bits = buf->decoder_info[2]; + this->audio_channels = buf->decoder_info[3]; + + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, "DV Audio"); + + this->decoder_ok = 1; + + return; + } + + if (this->decoder_ok && !(buf->decoder_flags & (BUF_FLAG_HEADER|BUF_FLAG_SPECIAL))) { + + if (!this->output_open) { + this->output_open = this->stream->audio_out->open(this->stream->audio_out, + this->stream, this->audio_bits, this->audio_sample_rate, + _x_ao_channels2mode(this->audio_channels)); + } + + /* if the audio still isn't open, bail */ + if (!this->output_open) + return; + + if( this->size + buf->size > this->bufsize ) { + this->bufsize = this->size + 2 * buf->size; + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + _("dvaudio: increasing 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_FRAME_END) { /* time to decode a frame */ + + offset = 0; + while (this->size>0) { + decode_buffer_size = dv_extract_audio(&this->buf[offset], this->decode_buffer, NULL); + + if (decode_buffer_size > -1) + bytes_consumed = dv_frame_profile(&this->buf[offset])->frame_size; + else + bytes_consumed = decode_buffer_size; + + /* dispatch the decoded audio */ + out = 0; + while (out < decode_buffer_size) { + audio_buffer = + this->stream->audio_out->get_buffer (this->stream->audio_out); + if (audio_buffer->mem_size == 0) { + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, + "dvaudio: Help! Allocated audio buffer with nothing in it!\n"); + return; + } + + if ((decode_buffer_size - out) > audio_buffer->mem_size) + bytes_to_send = audio_buffer->mem_size; + else + bytes_to_send = decode_buffer_size - out; + + /* fill up this buffer */ + xine_fast_memcpy(audio_buffer->mem, &this->decode_buffer[out], + bytes_to_send); + /* byte count / 2 (bytes / sample) / channels */ + audio_buffer->num_frames = bytes_to_send / 2 / this->audio_channels; + + audio_buffer->vpts = buf->pts; + buf->pts = 0; /* only first buffer gets the real pts */ + this->stream->audio_out->put_buffer (this->stream->audio_out, + audio_buffer, this->stream); + + out += bytes_to_send; + } + + this->size -= bytes_consumed; + offset += bytes_consumed; + } + + /* reset internal accumulation buffer */ + this->size = 0; + } + } +} + +static void dvaudio_reset (audio_decoder_t *this_gen) { + dvaudio_decoder_t *this = (dvaudio_decoder_t *) this_gen; + + this->size = 0; +} + +static void dvaudio_discontinuity (audio_decoder_t *this_gen) { +} + +static void dvaudio_dispose (audio_decoder_t *this_gen) { + + dvaudio_decoder_t *this = (dvaudio_decoder_t *) this_gen; + + if (this->output_open) + this->stream->audio_out->close (this->stream->audio_out, this->stream); + this->output_open = 0; + + free(this->buf); + free(this->decode_buffer); + + free (this_gen); +} + +static audio_decoder_t *dvaudio_open_plugin (audio_decoder_class_t *class_gen, xine_stream_t *stream) { + + dvaudio_decoder_t *this ; + + this = (dvaudio_decoder_t *) xine_xmalloc (sizeof (dvaudio_decoder_t)); + + this->audio_decoder.decode_data = dvaudio_decode_data; + this->audio_decoder.reset = dvaudio_reset; + this->audio_decoder.discontinuity = dvaudio_discontinuity; + this->audio_decoder.dispose = dvaudio_dispose; + + this->output_open = 0; + this->audio_channels = 0; + this->stream = stream; + this->buf = NULL; + this->size = 0; + this->decoder_ok = 0; + + return &this->audio_decoder; +} + +static char *dvaudio_get_identifier (audio_decoder_class_t *this) { + return "dv audio"; +} + +static char *dvaudio_get_description (audio_decoder_class_t *this) { + return "dv audio decoder plugin"; +} + +static void dvaudio_dispose_class (audio_decoder_class_t *this) { + free (this); +} + +static void *init_dvaudio_plugin (xine_t *xine, void *data) { + + dvaudio_class_t *this ; + + this = (dvaudio_class_t *) xine_xmalloc (sizeof (dvaudio_class_t)); + + this->decoder_class.open_plugin = dvaudio_open_plugin; + this->decoder_class.get_identifier = dvaudio_get_identifier; + this->decoder_class.get_description = dvaudio_get_description; + this->decoder_class.dispose = dvaudio_dispose_class; + + return this; +} + +static uint32_t supported_audio_types[] = { + BUF_AUDIO_DV, + 0 +}; + +static const decoder_info_t dec_info_dvaudio = { + supported_audio_types, /* supported types */ + 5 /* priority */ +}; + +/* + * exported plugin catalog entry + */ + +const plugin_info_t xine_plugin_info[] EXPORTED = { + /* type, API, "name", version, special_info, init_function */ + { PLUGIN_AUDIO_DECODER, 15, "dvaudio", XINE_VERSION_CODE, &dec_info_dvaudio, init_dvaudio_plugin }, + { PLUGIN_NONE, 0, "", 0, NULL, NULL } +}; diff --git a/src/libffmpeg/ff_mpeg_parser.c b/src/libffmpeg/ff_mpeg_parser.c new file mode 100644 index 000000000..fcee29a80 --- /dev/null +++ b/src/libffmpeg/ff_mpeg_parser.c @@ -0,0 +1,323 @@ +/* + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * Simple MPEG-ES parser/framer by Thibaut Mattern (tmattern@noos.fr) + * based on libmpeg2 decoder. + * + * $Id: mpeg_parser.c,v 1.7 2007/03/29 18:52:45 dgp85 Exp $ + */ +#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 = xine_xmalloc(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; +} diff --git a/src/libffmpeg/ff_mpeg_parser.h b/src/libffmpeg/ff_mpeg_parser.h new file mode 100644 index 000000000..b307714cc --- /dev/null +++ b/src/libffmpeg/ff_mpeg_parser.h @@ -0,0 +1,83 @@ +/* + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * Simple MPEG-ES parser/framer by Thibaut Mattern (tmattern@noos.fr) + * based on libmpeg2 decoder. + * + * $Id: mpeg_parser.h,v 1.5 2007/03/29 18:52:45 dgp85 Exp $ + */ +#ifndef HAVE_MPEG_PARSER_H +#define HAVE_MPEG_PARSER_H + +#include "xine_internal.h" +#include "ffmpeg_decoder.h" + +#define BUFFER_SIZE (1194 * 1024) /* libmpeg2's buffer size */ + +/* picture coding type (mpeg2 header) */ +#define I_TYPE 1 +#define P_TYPE 2 +#define B_TYPE 3 +#define D_TYPE 4 + +typedef struct mpeg_parser_s { + uint8_t *chunk_buffer; + uint8_t *chunk_ptr; + uint8_t *chunk_start; + uint32_t shift; + int buffer_size; + uint8_t code; + uint8_t picture_coding_type; + + uint8_t is_sequence_needed:1; + uint8_t is_mpeg1:1; /* public */ + uint8_t has_sequence:1; /* public */ + uint8_t in_slice:1; + + uint8_t rate_code:4; + + int aspect_ratio_info; + + /* public properties */ + uint16_t width; + uint16_t height; + int frame_duration; + double frame_aspect_ratio; + +} mpeg_parser_t; + +/* parser initialization */ +void mpeg_parser_init (mpeg_parser_t *parser); + +/* parser disposal */ +void mpeg_parser_dispose (mpeg_parser_t *parser); + +/* read a frame + * return a pointer to the first byte of the next frame + * or NULL if more bytes are needed + * *flush is set to 1 if the decoder must be flushed (needed for still menus) + */ +uint8_t *mpeg_parser_decode_data (mpeg_parser_t *parser, + uint8_t *current, uint8_t *end, + int *flush); + +/* reset the parser */ +void mpeg_parser_reset (mpeg_parser_t *parser); + +#endif /* HAVE_MPEG_PARSER_H */ diff --git a/src/libffmpeg/ff_video_decoder.c b/src/libffmpeg/ff_video_decoder.c new file mode 100644 index 000000000..e04c680b7 --- /dev/null +++ b/src/libffmpeg/ff_video_decoder.c @@ -0,0 +1,1772 @@ +/* + * Copyright (C) 2001-2007 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: video_decoder.c,v 1.73 2007/03/29 18:41:02 dgp85 Exp $ + * + * xine video decoder plugin using ffmpeg + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#include "ffmpeg_config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include + +#define LOG_MODULE "ffmpeg_video_dec" +#define LOG_VERBOSE +/* +#define LOG +*/ +#include "xine_internal.h" +#include "bswap.h" +#include "buffer.h" +#include "xineutils.h" +#include "ffmpeg_decoder.h" +#include "ff_mpeg_parser.h" + +#ifdef HAVE_FFMPEG +# include +#else +# include "libavcodec/libpostproc/postprocess.h" +#endif + +#define VIDEOBUFSIZE (128*1024) +#define SLICE_BUFFER_SIZE (1194*1024) + +#define SLICE_OFFSET_SIZE 128 + +#define ENABLE_DIRECT_RENDERING + +typedef struct ff_video_decoder_s ff_video_decoder_t; + +typedef struct ff_video_class_s { + video_decoder_class_t decoder_class; + + int pp_quality; + + xine_t *xine; +} ff_video_class_t; + +struct ff_video_decoder_s { + video_decoder_t video_decoder; + + ff_video_class_t *class; + + xine_stream_t *stream; + int64_t pts; + int video_step; + + uint8_t decoder_ok:1; + uint8_t decoder_init_mode:1; + uint8_t is_mpeg12:1; + uint8_t pp_available:1; + uint8_t yuv_init:1; + uint8_t is_direct_rendering_disabled:1; + uint8_t cs_convert_init:1; + + xine_bmiheader bih; + unsigned char *buf; + int bufsize; + int size; + int skipframes; + + int slice_offset_size; + + AVFrame *av_frame; + AVCodecContext *context; + AVCodec *codec; + + int pp_quality; + int pp_flags; + pp_context_t *pp_context; + pp_mode_t *pp_mode; + + /* mpeg-es parsing */ + mpeg_parser_t *mpeg_parser; + + double aspect_ratio; + int aspect_ratio_prio; + int frame_flags; + int crop_right, crop_bottom; + + int output_format; + + xine_list_t *dr1_frames; + + yuv_planes_t yuv; + + AVPaletteControl palette_control; +}; + + +static void set_stream_info(ff_video_decoder_t *this) { + _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_WIDTH, this->bih.biWidth); + _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_HEIGHT, this->bih.biHeight); + _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_RATIO, this->aspect_ratio * 10000); +} + +#ifdef ENABLE_DIRECT_RENDERING +/* called from ffmpeg to do direct rendering method 1 */ +static int get_buffer(AVCodecContext *context, AVFrame *av_frame){ + ff_video_decoder_t *this = (ff_video_decoder_t *)context->opaque; + vo_frame_t *img; + int width = context->width; + int height = context->height; + + if (!this->bih.biWidth || !this->bih.biHeight) { + this->bih.biWidth = width; + this->bih.biHeight = height; + + if (this->aspect_ratio_prio == 0) { + this->aspect_ratio = (double)width / (double)height; + this->aspect_ratio_prio = 1; + lprintf("default aspect ratio: %f\n", this->aspect_ratio); + set_stream_info(this); + } + } + + avcodec_align_dimensions(context, &width, &height); + + if( this->context->pix_fmt != PIX_FMT_YUV420P ) { + if (!this->is_direct_rendering_disabled) { + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + _("ffmpeg_video_dec: unsupported frame format, DR1 disabled.\n")); + this->is_direct_rendering_disabled = 1; + } + + /* FIXME: why should i have to do that ? */ + av_frame->data[0]= NULL; + av_frame->data[1]= NULL; + av_frame->data[2]= NULL; + return avcodec_default_get_buffer(context, av_frame); + } + + if((width != this->bih.biWidth) || (height != this->bih.biHeight)) { + if(this->stream->video_out->get_capabilities(this->stream->video_out) & VO_CAP_CROP) { + this->crop_right = width - this->bih.biWidth; + this->crop_bottom = height - this->bih.biHeight; + } else { + if (!this->is_direct_rendering_disabled) { + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + _("ffmpeg_video_dec: unsupported frame dimensions, DR1 disabled.\n")); + this->is_direct_rendering_disabled = 1; + } + /* FIXME: why should i have to do that ? */ + av_frame->data[0]= NULL; + av_frame->data[1]= NULL; + av_frame->data[2]= NULL; + return avcodec_default_get_buffer(context, av_frame); + } + } + + img = this->stream->video_out->get_frame (this->stream->video_out, + width, + height, + this->aspect_ratio, + this->output_format, + VO_BOTH_FIELDS|this->frame_flags); + + av_frame->opaque = img; + + av_frame->data[0]= img->base[0]; + av_frame->data[1]= img->base[1]; + av_frame->data[2]= img->base[2]; + + av_frame->linesize[0] = img->pitches[0]; + av_frame->linesize[1] = img->pitches[1]; + av_frame->linesize[2] = img->pitches[2]; + + /* We should really keep track of the ages of xine frames (see + * avcodec_default_get_buffer in libavcodec/utils.c) + * For the moment tell ffmpeg that every frame is new (age = bignumber) */ + av_frame->age = 256*256*256*64; + + av_frame->type= FF_BUFFER_TYPE_USER; + + xine_list_push_back(this->dr1_frames, av_frame); + + return 0; +} + +static void release_buffer(struct AVCodecContext *context, AVFrame *av_frame){ + ff_video_decoder_t *this = (ff_video_decoder_t *)context->opaque; + + if (av_frame->type == FF_BUFFER_TYPE_USER) { + vo_frame_t *img = (vo_frame_t *)av_frame->opaque; + xine_list_iterator_t it; + + assert(av_frame->opaque); + img->free(img); + + it = xine_list_find(this->dr1_frames, av_frame); + assert(it); + if( it != NULL ) + xine_list_remove(this->dr1_frames, it); + } else { + avcodec_default_release_buffer(context, av_frame); + } + + av_frame->opaque = NULL; + av_frame->data[0]= NULL; + av_frame->data[1]= NULL; + av_frame->data[2]= NULL; +} +#endif + +static const ff_codec_t ff_video_lookup[] = { + {BUF_VIDEO_MSMPEG4_V1, CODEC_ID_MSMPEG4V1, "Microsoft MPEG-4 v1 (ffmpeg)"}, + {BUF_VIDEO_MSMPEG4_V2, CODEC_ID_MSMPEG4V2, "Microsoft MPEG-4 v2 (ffmpeg)"}, + {BUF_VIDEO_MSMPEG4_V3, CODEC_ID_MSMPEG4V3, "Microsoft MPEG-4 v3 (ffmpeg)"}, + {BUF_VIDEO_WMV7, CODEC_ID_WMV1, "MS Windows Media Video 7 (ffmpeg)"}, + {BUF_VIDEO_WMV8, CODEC_ID_WMV2, "MS Windows Media Video 8 (ffmpeg)"}, + {BUF_VIDEO_WMV9, CODEC_ID_WMV3, "MS Windows Media Video 9 (ffmpeg)"}, + {BUF_VIDEO_MPEG4, CODEC_ID_MPEG4, "ISO MPEG-4 (ffmpeg)"}, + {BUF_VIDEO_XVID, CODEC_ID_MPEG4, "ISO MPEG-4 (XviD, ffmpeg)"}, + {BUF_VIDEO_DIVX5, CODEC_ID_MPEG4, "ISO MPEG-4 (DivX5, ffmpeg)"}, + {BUF_VIDEO_3IVX, CODEC_ID_MPEG4, "ISO MPEG-4 (3ivx, ffmpeg)"}, + {BUF_VIDEO_JPEG, CODEC_ID_MJPEG, "Motion JPEG (ffmpeg)"}, + {BUF_VIDEO_MJPEG, CODEC_ID_MJPEG, "Motion JPEG (ffmpeg)"}, + {BUF_VIDEO_MJPEG_B, CODEC_ID_MJPEGB, "Motion JPEG B (ffmpeg)"}, + {BUF_VIDEO_I263, CODEC_ID_H263I, "ITU H.263 (ffmpeg)"}, + {BUF_VIDEO_H263, CODEC_ID_H263, "H.263 (ffmpeg)"}, + {BUF_VIDEO_RV10, CODEC_ID_RV10, "Real Video 1.0 (ffmpeg)"}, + {BUF_VIDEO_RV20, CODEC_ID_RV20, "Real Video 2.0 (ffmpeg)"}, + {BUF_VIDEO_IV31, CODEC_ID_INDEO3, "Indeo Video 3.1 (ffmpeg)"}, + {BUF_VIDEO_IV32, CODEC_ID_INDEO3, "Indeo Video 3.2 (ffmpeg)"}, + {BUF_VIDEO_SORENSON_V1, CODEC_ID_SVQ1, "Sorenson Video 1 (ffmpeg)"}, + {BUF_VIDEO_SORENSON_V3, CODEC_ID_SVQ3, "Sorenson Video 3 (ffmpeg)"}, + {BUF_VIDEO_DV, CODEC_ID_DVVIDEO, "DV (ffmpeg)"}, + {BUF_VIDEO_HUFFYUV, CODEC_ID_HUFFYUV, "HuffYUV (ffmpeg)"}, + {BUF_VIDEO_VP31, CODEC_ID_VP3, "On2 VP3.1 (ffmpeg)"}, + {BUF_VIDEO_VP5, CODEC_ID_VP5, "On2 VP5 (ffmpeg)"}, + {BUF_VIDEO_VP6, CODEC_ID_VP6, "On2 VP6 (ffmpeg)"}, + {BUF_VIDEO_VP6F, CODEC_ID_VP6F, "On2 VP6 (ffmpeg)"}, + {BUF_VIDEO_4XM, CODEC_ID_4XM, "4X Video (ffmpeg)"}, + {BUF_VIDEO_CINEPAK, CODEC_ID_CINEPAK, "Cinepak (ffmpeg)"}, + {BUF_VIDEO_MSVC, CODEC_ID_MSVIDEO1, "Microsoft Video 1 (ffmpeg)"}, + {BUF_VIDEO_MSRLE, CODEC_ID_MSRLE, "Microsoft RLE (ffmpeg)"}, + {BUF_VIDEO_RPZA, CODEC_ID_RPZA, "Apple Quicktime Video/RPZA (ffmpeg)"}, + {BUF_VIDEO_CYUV, CODEC_ID_CYUV, "Creative YUV (ffmpeg)"}, + {BUF_VIDEO_ROQ, CODEC_ID_ROQ, "Id Software RoQ (ffmpeg)"}, + {BUF_VIDEO_IDCIN, CODEC_ID_IDCIN, "Id Software CIN (ffmpeg)"}, + {BUF_VIDEO_WC3, CODEC_ID_XAN_WC3, "Xan (ffmpeg)"}, + {BUF_VIDEO_VQA, CODEC_ID_WS_VQA, "Westwood Studios VQA (ffmpeg)"}, + {BUF_VIDEO_INTERPLAY, CODEC_ID_INTERPLAY_VIDEO, "Interplay MVE (ffmpeg)"}, + {BUF_VIDEO_FLI, CODEC_ID_FLIC, "FLIC Video (ffmpeg)"}, + {BUF_VIDEO_8BPS, CODEC_ID_8BPS, "Planar RGB (ffmpeg)"}, + {BUF_VIDEO_SMC, CODEC_ID_SMC, "Apple Quicktime Graphics/SMC (ffmpeg)"}, + {BUF_VIDEO_DUCKTM1, CODEC_ID_TRUEMOTION1,"Duck TrueMotion v1 (ffmpeg)"}, + {BUF_VIDEO_DUCKTM2, CODEC_ID_TRUEMOTION2,"Duck TrueMotion v2 (ffmpeg)"}, + {BUF_VIDEO_VMD, CODEC_ID_VMDVIDEO, "Sierra VMD Video (ffmpeg)"}, + {BUF_VIDEO_ZLIB, CODEC_ID_ZLIB, "ZLIB Video (ffmpeg)"}, + {BUF_VIDEO_MSZH, CODEC_ID_MSZH, "MSZH Video (ffmpeg)"}, + {BUF_VIDEO_ASV1, CODEC_ID_ASV1, "ASV v1 Video (ffmpeg)"}, + {BUF_VIDEO_ASV2, CODEC_ID_ASV2, "ASV v2 Video (ffmpeg)"}, + {BUF_VIDEO_ATIVCR1, CODEC_ID_VCR1, "ATI VCR-1 (ffmpeg)"}, + {BUF_VIDEO_FLV1, CODEC_ID_FLV1, "Flash Video (ffmpeg)"}, + {BUF_VIDEO_QTRLE, CODEC_ID_QTRLE, "Apple Quicktime Animation/RLE (ffmpeg)"}, + {BUF_VIDEO_H264, CODEC_ID_H264, "H.264/AVC (ffmpeg)"}, + {BUF_VIDEO_H261, CODEC_ID_H261, "H.261 (ffmpeg)"}, + {BUF_VIDEO_AASC, CODEC_ID_AASC, "Autodesk Video (ffmpeg)"}, + {BUF_VIDEO_LOCO, CODEC_ID_LOCO, "LOCO (ffmpeg)"}, + {BUF_VIDEO_QDRW, CODEC_ID_QDRAW, "QuickDraw (ffmpeg)"}, + {BUF_VIDEO_QPEG, CODEC_ID_QPEG, "Q-Team QPEG (ffmpeg)"}, + {BUF_VIDEO_TSCC, CODEC_ID_TSCC, "TechSmith Video (ffmpeg)"}, + {BUF_VIDEO_ULTI, CODEC_ID_ULTI, "IBM UltiMotion (ffmpeg)"}, + {BUF_VIDEO_WNV1, CODEC_ID_WNV1, "Winnow Video (ffmpeg)"}, + {BUF_VIDEO_XL, CODEC_ID_VIXL, "Miro/Pinnacle VideoXL (ffmpeg)"}, + {BUF_VIDEO_RT21, CODEC_ID_INDEO2, "Indeo/RealTime 2 (ffmpeg)"}, + {BUF_VIDEO_FPS1, CODEC_ID_FRAPS, "Fraps (ffmpeg)"}, + {BUF_VIDEO_MPEG, CODEC_ID_MPEG1VIDEO, "MPEG 1/2 (ffmpeg)"}, + {BUF_VIDEO_CSCD, CODEC_ID_CSCD, "CamStudio (ffmpeg)"}, + {BUF_VIDEO_AVS, CODEC_ID_AVS, "AVS (ffmpeg)"}, + {BUF_VIDEO_ALGMM, CODEC_ID_MMVIDEO, "American Laser Games MM (ffmpeg)"}, + {BUF_VIDEO_ZMBV, CODEC_ID_ZMBV, "Zip Motion Blocks Video (ffmpeg)"}, + {BUF_VIDEO_SMACKER, CODEC_ID_SMACKVIDEO, "Smacker (ffmpeg)"}, + {BUF_VIDEO_NUV, CODEC_ID_NUV, "NuppelVideo (ffmpeg)"}, + {BUF_VIDEO_KMVC, CODEC_ID_KMVC, "Karl Morton's Video Codec (ffmpeg)"}, + {BUF_VIDEO_FLASHSV, CODEC_ID_FLASHSV, "Flash Screen Video (ffmpeg)"}, + {BUF_VIDEO_CAVS, CODEC_ID_CAVS, "Chinese AVS (ffmpeg)"}, +}; + + +static void init_video_codec (ff_video_decoder_t *this, unsigned int codec_type) { + size_t i; + + /* find the decoder */ + this->codec = NULL; + + for(i = 0; i < sizeof(ff_video_lookup)/sizeof(ff_codec_t); i++) + if(ff_video_lookup[i].type == codec_type) { + pthread_mutex_lock(&ffmpeg_lock); + this->codec = avcodec_find_decoder(ff_video_lookup[i].id); + pthread_mutex_unlock(&ffmpeg_lock); + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_VIDEOCODEC, + ff_video_lookup[i].name); + break; + } + + if (!this->codec) { + xprintf (this->stream->xine, XINE_VERBOSITY_LOG, + _("ffmpeg_video_dec: couldn't find ffmpeg decoder for buf type 0x%X\n"), + codec_type); + _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_HANDLED, 0); + return; + } + + lprintf("lavc decoder found\n"); + + /* force (width % 8 == 0), otherwise there will be + * display problems with Xv. + */ + this->bih.biWidth = (this->bih.biWidth + 1) & (~1); + + this->context->width = this->bih.biWidth; + this->context->height = this->bih.biHeight; + this->context->stream_codec_tag = this->context->codec_tag = + _x_stream_info_get(this->stream, XINE_STREAM_INFO_VIDEO_FOURCC); + + + /* Some codecs (eg rv10) copy flags in init so it's necessary to set + * this flag here in case we are going to use direct rendering */ + if(this->codec->capabilities & CODEC_CAP_DR1) { + this->context->flags |= CODEC_FLAG_EMU_EDGE; + } + + pthread_mutex_lock(&ffmpeg_lock); + if (avcodec_open (this->context, this->codec) < 0) { + pthread_mutex_unlock(&ffmpeg_lock); + xprintf (this->stream->xine, XINE_VERBOSITY_LOG, + _("ffmpeg_video_dec: couldn't open decoder\n")); + free(this->context); + this->context = NULL; + _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_HANDLED, 0); + return; + } + pthread_mutex_unlock(&ffmpeg_lock); + + lprintf("lavc decoder opened\n"); + + this->decoder_ok = 1; + + if ((codec_type != BUF_VIDEO_MPEG) && + (codec_type != BUF_VIDEO_DV)) { + + if (!this->bih.biWidth || !this->bih.biHeight) { + this->bih.biWidth = this->context->width; + this->bih.biHeight = this->context->height; + } + + + set_stream_info(this); + } + + this->stream->video_out->open (this->stream->video_out, this->stream); + + this->skipframes = 0; + + /* enable direct rendering by default */ + this->output_format = XINE_IMGFMT_YV12; +#ifdef ENABLE_DIRECT_RENDERING + if( this->codec->capabilities & CODEC_CAP_DR1 && this->codec->id != CODEC_ID_H264 ) { + this->context->get_buffer = get_buffer; + this->context->release_buffer = release_buffer; + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + _("ffmpeg_video_dec: direct rendering enabled\n")); + } +#endif + + /* flag for interlaced streams */ + this->frame_flags = 0; + /* FIXME: which codecs can be interlaced? + FIXME: check interlaced DCT and other codec specific info. */ + switch( codec_type ) { + case BUF_VIDEO_DV: + this->frame_flags |= VO_INTERLACED_FLAG; + break; + case BUF_VIDEO_MPEG: + this->frame_flags |= VO_INTERLACED_FLAG; + break; + case BUF_VIDEO_MJPEG: + this->frame_flags |= VO_INTERLACED_FLAG; + break; + case BUF_VIDEO_HUFFYUV: + this->frame_flags |= VO_INTERLACED_FLAG; + break; + } + +} + +static void pp_quality_cb(void *user_data, xine_cfg_entry_t *entry) { + ff_video_class_t *class = (ff_video_class_t *) user_data; + + class->pp_quality = entry->num_value; +} + +static void pp_change_quality (ff_video_decoder_t *this) { + this->pp_quality = this->class->pp_quality; + + if(this->pp_available && this->pp_quality) { + if(!this->pp_context && this->context) + this->pp_context = pp_get_context(this->context->width, this->context->height, + this->pp_flags); + if(this->pp_mode) + pp_free_mode(this->pp_mode); + + this->pp_mode = pp_get_mode_by_name_and_quality("hb:a,vb:a,dr:a", + this->pp_quality); + } else { + if(this->pp_mode) { + pp_free_mode(this->pp_mode); + this->pp_mode = NULL; + } + + if(this->pp_context) { + pp_free_context(this->pp_context); + this->pp_context = NULL; + } + } +} + +static void init_postprocess (ff_video_decoder_t *this) { + uint32_t cpu_caps; + + /* Allow post processing on mpeg-4 (based) codecs */ + switch(this->codec->id) { + case CODEC_ID_MPEG4: + case CODEC_ID_MSMPEG4V1: + case CODEC_ID_MSMPEG4V2: + case CODEC_ID_MSMPEG4V3: + case CODEC_ID_WMV1: + case CODEC_ID_WMV2: + this->pp_available = 1; + break; + default: + this->pp_available = 0; + break; + } + + /* Detect what cpu accel we have */ + cpu_caps = xine_mm_accel(); + this->pp_flags = PP_FORMAT_420; + + if(cpu_caps & MM_ACCEL_X86_MMX) + this->pp_flags |= PP_CPU_CAPS_MMX; + + if(cpu_caps & MM_ACCEL_X86_MMXEXT) + this->pp_flags |= PP_CPU_CAPS_MMX2; + + if(cpu_caps & MM_ACCEL_X86_3DNOW) + this->pp_flags |= PP_CPU_CAPS_3DNOW; + + /* Set level */ + pp_change_quality(this); +} + +static int ff_handle_mpeg_sequence(ff_video_decoder_t *this, mpeg_parser_t *parser) { + + /* + * init codec + */ + if (this->decoder_init_mode) { + _x_meta_info_set_utf8(this->stream, XINE_META_INFO_VIDEOCODEC, + "mpeg-1 (ffmpeg)"); + + init_video_codec (this, BUF_VIDEO_MPEG); + this->decoder_init_mode = 0; + } + + /* frame format change */ + if ((parser->width != this->bih.biWidth) || + (parser->height != this->bih.biHeight) || + (parser->frame_aspect_ratio != this->aspect_ratio)) { + xine_event_t event; + xine_format_change_data_t data; + + this->bih.biWidth = parser->width; + this->bih.biHeight = parser->height; + this->aspect_ratio = parser->frame_aspect_ratio; + this->aspect_ratio_prio = 2; + lprintf("mpeg seq aspect ratio: %f\n", this->aspect_ratio); + set_stream_info(this); + + event.type = XINE_EVENT_FRAME_FORMAT_CHANGE; + event.stream = this->stream; + event.data = &data; + event.data_length = sizeof(data); + data.width = this->bih.biWidth; + data.height = this->bih.biHeight; + data.aspect = this->aspect_ratio; + data.pan_scan = 0; + xine_event_send(this->stream, &event); + } + this->video_step = this->mpeg_parser->frame_duration; + + return 1; +} + +static void ff_convert_frame(ff_video_decoder_t *this, vo_frame_t *img) { + int y; + uint8_t *dy, *du, *dv, *sy, *su, *sv; + + dy = img->base[0]; + du = img->base[1]; + dv = img->base[2]; + sy = this->av_frame->data[0]; + su = this->av_frame->data[1]; + sv = this->av_frame->data[2]; + + if (this->context->pix_fmt == PIX_FMT_YUV410P) { + + yuv9_to_yv12( + /* Y */ + this->av_frame->data[0], + this->av_frame->linesize[0], + img->base[0], + img->pitches[0], + /* U */ + this->av_frame->data[1], + this->av_frame->linesize[1], + img->base[1], + img->pitches[1], + /* V */ + this->av_frame->data[2], + this->av_frame->linesize[2], + img->base[2], + img->pitches[2], + /* width x height */ + img->width, + img->height); + + } else if (this->context->pix_fmt == PIX_FMT_YUV411P) { + + yuv411_to_yv12( + /* Y */ + this->av_frame->data[0], + this->av_frame->linesize[0], + img->base[0], + img->pitches[0], + /* U */ + this->av_frame->data[1], + this->av_frame->linesize[1], + img->base[1], + img->pitches[1], + /* V */ + this->av_frame->data[2], + this->av_frame->linesize[2], + img->base[2], + img->pitches[2], + /* width x height */ + img->width, + img->height); + + } else if (this->context->pix_fmt == PIX_FMT_RGBA32) { + + int x, plane_ptr = 0; + uint32_t *argb_pixels; + uint32_t argb; + + for(y = 0; y < img->height; y++) { + argb_pixels = (uint32_t *)sy; + for(x = 0; x < img->width; x++) { + uint8_t r, g, b; + + /* this is endian-safe as the ARGB pixels are stored in + * machine order */ + argb = *argb_pixels++; + r = (argb >> 16) & 0xFF; + g = (argb >> 8) & 0xFF; + b = (argb >> 0) & 0xFF; + + this->yuv.y[plane_ptr] = COMPUTE_Y(r, g, b); + this->yuv.u[plane_ptr] = COMPUTE_U(r, g, b); + this->yuv.v[plane_ptr] = COMPUTE_V(r, g, b); + plane_ptr++; + } + sy += this->av_frame->linesize[0]; + } + + yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); + + } else if (this->context->pix_fmt == PIX_FMT_RGB565) { + + int x, plane_ptr = 0; + uint8_t *src; + uint16_t pixel16; + + for(y = 0; y < img->height; y++) { + src = sy; + for(x = 0; x < img->width; x++) { + uint8_t r, g, b; + + /* a 16-bit RGB565 pixel is supposed to be stored in native-endian + * byte order; the following should be endian-safe */ + pixel16 = *((uint16_t *)src); + src += 2; + b = (pixel16 << 3) & 0xFF; + g = (pixel16 >> 3) & 0xFF; + r = (pixel16 >> 8) & 0xFF; + + this->yuv.y[plane_ptr] = COMPUTE_Y(r, g, b); + this->yuv.u[plane_ptr] = COMPUTE_U(r, g, b); + this->yuv.v[plane_ptr] = COMPUTE_V(r, g, b); + plane_ptr++; + } + sy += this->av_frame->linesize[0]; + } + + yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); + + } else if (this->context->pix_fmt == PIX_FMT_RGB555) { + + int x, plane_ptr = 0; + uint8_t *src; + uint16_t pixel16; + + for(y = 0; y < img->height; y++) { + src = sy; + for(x = 0; x < img->width; x++) { + uint8_t r, g, b; + + /* a 16-bit RGB555 pixel is supposed to be stored in native-endian + * byte order; the following should be endian-safe */ + pixel16 = *((uint16_t *)src); + src += 2; + b = (pixel16 << 3) & 0xFF; + g = (pixel16 >> 2) & 0xFF; + r = (pixel16 >> 7) & 0xFF; + + this->yuv.y[plane_ptr] = COMPUTE_Y(r, g, b); + this->yuv.u[plane_ptr] = COMPUTE_U(r, g, b); + this->yuv.v[plane_ptr] = COMPUTE_V(r, g, b); + plane_ptr++; + } + sy += this->av_frame->linesize[0]; + } + + yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); + + } else if (this->context->pix_fmt == PIX_FMT_BGR24) { + + int x, plane_ptr = 0; + uint8_t *src; + + for(y = 0; y < img->height; y++) { + src = sy; + for(x = 0; x < img->width; x++) { + uint8_t r, g, b; + + b = *src++; + g = *src++; + r = *src++; + + this->yuv.y[plane_ptr] = COMPUTE_Y(r, g, b); + this->yuv.u[plane_ptr] = COMPUTE_U(r, g, b); + this->yuv.v[plane_ptr] = COMPUTE_V(r, g, b); + plane_ptr++; + } + sy += this->av_frame->linesize[0]; + } + + yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); + + } else if (this->context->pix_fmt == PIX_FMT_RGB24) { + + int x, plane_ptr = 0; + uint8_t *src; + + for(y = 0; y < img->height; y++) { + src = sy; + for(x = 0; x < img->width; x++) { + uint8_t r, g, b; + + r = *src++; + g = *src++; + b = *src++; + + this->yuv.y[plane_ptr] = COMPUTE_Y(r, g, b); + this->yuv.u[plane_ptr] = COMPUTE_U(r, g, b); + this->yuv.v[plane_ptr] = COMPUTE_V(r, g, b); + plane_ptr++; + } + sy += this->av_frame->linesize[0]; + } + + yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); + + } else if (this->context->pix_fmt == PIX_FMT_PAL8) { + + int x, plane_ptr = 0; + uint8_t *src; + uint8_t pixel; + uint32_t *palette32 = (uint32_t *)su; /* palette is in data[1] */ + uint32_t rgb_color; + uint8_t r, g, b; + uint8_t y_palette[256]; + uint8_t u_palette[256]; + uint8_t v_palette[256]; + + for (x = 0; x < 256; x++) { + rgb_color = palette32[x]; + b = rgb_color & 0xFF; + rgb_color >>= 8; + g = rgb_color & 0xFF; + rgb_color >>= 8; + r = rgb_color & 0xFF; + y_palette[x] = COMPUTE_Y(r, g, b); + u_palette[x] = COMPUTE_U(r, g, b); + v_palette[x] = COMPUTE_V(r, g, b); + } + + for(y = 0; y < img->height; y++) { + src = sy; + for(x = 0; x < img->width; x++) { + pixel = *src++; + + this->yuv.y[plane_ptr] = y_palette[pixel]; + this->yuv.u[plane_ptr] = u_palette[pixel]; + this->yuv.v[plane_ptr] = v_palette[pixel]; + plane_ptr++; + } + sy += this->av_frame->linesize[0]; + } + + yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); + + } else { + + for (y=0; yheight; y++) { + xine_fast_memcpy (dy, sy, img->width); + + dy += img->pitches[0]; + + sy += this->av_frame->linesize[0]; + } + + for (y=0; y<(img->height/2); y++) { + + if (this->context->pix_fmt != PIX_FMT_YUV444P) { + + xine_fast_memcpy (du, su, img->width/2); + xine_fast_memcpy (dv, sv, img->width/2); + + } else { + + int x; + uint8_t *src; + uint8_t *dst; + + /* subsample */ + + src = su; dst = du; + for (x=0; x<(img->width/2); x++) { + *dst = *src; + dst++; + src += 2; + } + src = sv; dst = dv; + for (x=0; x<(img->width/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_frame->linesize[1]; + sv += 2*this->av_frame->linesize[2]; + } else { + su += this->av_frame->linesize[1]; + sv += this->av_frame->linesize[2]; + } + } + } +} + +static void ff_check_bufsize (ff_video_decoder_t *this, int size) { + if (size > this->bufsize) { + this->bufsize = size + size / 2; + xprintf(this->stream->xine, XINE_VERBOSITY_LOG, + _("ffmpeg_video_dec: increasing buffer to %d to avoid overflow.\n"), + this->bufsize); + this->buf = realloc(this->buf, this->bufsize + FF_INPUT_BUFFER_PADDING_SIZE ); + } +} + +static void ff_handle_preview_buffer (ff_video_decoder_t *this, buf_element_t *buf) { + int codec_type; + + lprintf ("preview buffer\n"); + + codec_type = buf->type & 0xFFFF0000; + if (codec_type == BUF_VIDEO_MPEG) { + this->is_mpeg12 = 1; + if ( this->mpeg_parser == NULL ) { + this->mpeg_parser = xine_xmalloc(sizeof(mpeg_parser_t)); + mpeg_parser_init(this->mpeg_parser); + this->decoder_init_mode = 0; + } + } + + if (this->decoder_init_mode && !this->is_mpeg12) { + init_video_codec(this, codec_type); + init_postprocess(this); + this->decoder_init_mode = 0; + } +} + +static void ff_handle_header_buffer (ff_video_decoder_t *this, buf_element_t *buf) { + + lprintf ("header buffer\n"); + + /* accumulate data */ + ff_check_bufsize(this, this->size + buf->size); + xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size); + this->size += buf->size; + + if (buf->decoder_flags & BUF_FLAG_FRAME_END) { + int codec_type; + + lprintf ("header complete\n"); + codec_type = buf->type & 0xFFFF0000; + + if (buf->decoder_flags & BUF_FLAG_STDHEADER) { + + lprintf("standard header\n"); + + /* init package containing bih */ + memcpy ( &this->bih, this->buf, sizeof(xine_bmiheader) ); + + if (this->bih.biSize > sizeof(xine_bmiheader)) { + this->context->extradata_size = this->bih.biSize - sizeof(xine_bmiheader); + this->context->extradata = malloc(this->context->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + memcpy(this->context->extradata, this->buf + sizeof(xine_bmiheader), + this->context->extradata_size); + } + + this->context->bits_per_sample = this->bih.biBitCount; + + } else { + + switch (codec_type) { + case BUF_VIDEO_RV10: + case BUF_VIDEO_RV20: + this->bih.biWidth = BE_16(&this->buf[12]); + this->bih.biHeight = BE_16(&this->buf[14]); + + this->context->sub_id = BE_32(&this->buf[30]); + + this->context->slice_offset = xine_xmalloc(sizeof(int)*SLICE_OFFSET_SIZE); + this->slice_offset_size = SLICE_OFFSET_SIZE; + + lprintf("w=%d, h=%d\n", this->bih.biWidth, this->bih.biHeight); + + break; + default: + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "ffmpeg_video_dec: unknown header for buf type 0x%X\n", codec_type); + return; + } + } + + /* reset accumulator */ + this->size = 0; + } +} + +static void ff_handle_special_buffer (ff_video_decoder_t *this, buf_element_t *buf) { + /* take care of all the various types of special buffers + * note that order is important here */ + lprintf("special buffer\n"); + + if (buf->decoder_info[1] == BUF_SPECIAL_STSD_ATOM && + !this->context->extradata_size) { + + lprintf("BUF_SPECIAL_STSD_ATOM\n"); + this->context->extradata_size = buf->decoder_info[2]; + this->context->extradata = xine_xmalloc(buf->decoder_info[2] + + FF_INPUT_BUFFER_PADDING_SIZE); + memcpy(this->context->extradata, buf->decoder_info_ptr[2], + buf->decoder_info[2]); + + } else if (buf->decoder_info[1] == BUF_SPECIAL_DECODER_CONFIG && + !this->context->extradata_size) { + + lprintf("BUF_SPECIAL_DECODER_CONFIG\n"); + this->context->extradata_size = buf->decoder_info[2]; + this->context->extradata = xine_xmalloc(buf->decoder_info[2] + + FF_INPUT_BUFFER_PADDING_SIZE); + memcpy(this->context->extradata, buf->decoder_info_ptr[2], + buf->decoder_info[2]); + + } else if (buf->decoder_info[1] == BUF_SPECIAL_PALETTE) { + unsigned int i; + + palette_entry_t *demuxer_palette; + AVPaletteControl *decoder_palette; + + lprintf("BUF_SPECIAL_PALETTE\n"); + this->context->palctrl = &this->palette_control; + decoder_palette = (AVPaletteControl *)this->context->palctrl; + demuxer_palette = (palette_entry_t *)buf->decoder_info_ptr[2]; + + for (i = 0; i < buf->decoder_info[2]; i++) { + decoder_palette->palette[i] = + (demuxer_palette[i].r << 16) | + (demuxer_palette[i].g << 8) | + (demuxer_palette[i].b << 0); + } + decoder_palette->palette_changed = 1; + + } else if (buf->decoder_info[1] == BUF_SPECIAL_RV_CHUNK_TABLE) { + int i; + + lprintf("BUF_SPECIAL_RV_CHUNK_TABLE\n"); + this->context->slice_count = buf->decoder_info[2]+1; + + lprintf("slice_count=%d\n", this->context->slice_count); + + if(this->context->slice_count > this->slice_offset_size) { + this->context->slice_offset = realloc(this->context->slice_offset, + sizeof(int)*this->context->slice_count); + this->slice_offset_size = this->context->slice_count; + } + + for(i = 0; i < this->context->slice_count; i++) { + this->context->slice_offset[i] = + ((uint32_t *) buf->decoder_info_ptr[2])[(2*i)+1]; + lprintf("slice_offset[%d]=%d\n", i, this->context->slice_offset[i]); + } + } +} + +static void ff_handle_mpeg12_buffer (ff_video_decoder_t *this, buf_element_t *buf) { + + vo_frame_t *img; + int free_img; + int got_picture, len; + int offset = 0; + int flush = 0; + int size = buf->size; + + lprintf("handle_mpeg12_buffer\n"); + + while ((size > 0) || (flush == 1)) { + + uint8_t *current; + int next_flush; + + got_picture = 0; + if (!flush) { + current = mpeg_parser_decode_data(this->mpeg_parser, + buf->content + offset, buf->content + offset + size, + &next_flush); + } else { + current = buf->content + offset + size; /* end of the buffer */ + next_flush = 0; + } + if (current == NULL) { + lprintf("current == NULL\n"); + return; + } + + if (this->mpeg_parser->has_sequence) { + ff_handle_mpeg_sequence(this, this->mpeg_parser); + } + + if (!this->decoder_ok) + return; + + if (flush) { + lprintf("flush lavc buffers\n"); + /* hack: ffmpeg outputs the last frame if size=0 */ + this->mpeg_parser->buffer_size = 0; + } + + /* skip decoding b frames if too late */ + this->context->hurry_up = (this->skipframes > 0); + + lprintf("avcodec_decode_video: size=%d\n", this->mpeg_parser->buffer_size); + len = avcodec_decode_video (this->context, this->av_frame, + &got_picture, this->mpeg_parser->chunk_buffer, + this->mpeg_parser->buffer_size); + lprintf("avcodec_decode_video: decoded_size=%d, got_picture=%d\n", + len, got_picture); + len = current - buf->content - offset; + lprintf("avcodec_decode_video: consumed_size=%d\n", len); + + flush = next_flush; + + if ((len < 0) || (len > buf->size)) { + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, + "ffmpeg_video_dec: error decompressing frame\n"); + size = 0; /* draw a bad frame and exit */ + } else { + size -= len; + offset += len; + } + + if (got_picture && this->av_frame->data[0]) { + /* got a picture, draw it */ + if(!this->av_frame->opaque) { + /* indirect rendering */ + img = this->stream->video_out->get_frame (this->stream->video_out, + this->bih.biWidth, + this->bih.biHeight, + this->aspect_ratio, + this->output_format, + VO_BOTH_FIELDS|this->frame_flags); + free_img = 1; + } else { + /* DR1 */ + img = (vo_frame_t*) this->av_frame->opaque; + free_img = 0; + } + + img->pts = this->pts; + this->pts = 0; + + if (this->av_frame->repeat_pict) + img->duration = this->video_step * 3 / 2; + else + img->duration = this->video_step; + + img->crop_right = this->crop_right; + img->crop_bottom = this->crop_bottom; + + this->skipframes = img->draw(img, this->stream); + + if(free_img) + img->free(img); + + } else { + + if (this->context->hurry_up) { + /* skipped frame, output a bad frame */ + img = this->stream->video_out->get_frame (this->stream->video_out, + this->bih.biWidth, + this->bih.biHeight, + this->aspect_ratio, + this->output_format, + VO_BOTH_FIELDS|this->frame_flags); + img->pts = 0; + img->duration = this->video_step; + img->bad_frame = 1; + this->skipframes = img->draw(img, this->stream); + img->free(img); + } + } + } +} + +static void ff_handle_buffer (ff_video_decoder_t *this, buf_element_t *buf) { + uint8_t *chunk_buf = this->buf; + AVRational avr00 = {0, 1}; + + lprintf("handle_buffer\n"); + + if (!this->decoder_ok) { + if (this->decoder_init_mode) { + int codec_type = buf->type & 0xFFFF0000; + + /* init ffmpeg decoder */ + init_video_codec(this, codec_type); + init_postprocess(this); + this->decoder_init_mode = 0; + } else { + return; + } + } + + if (buf->decoder_flags & BUF_FLAG_FRAME_START) { + lprintf("BUF_FLAG_FRAME_START\n"); + this->size = 0; + } + + /* data accumulation */ + if (buf->size > 0) { + if ((this->size == 0) && + ((buf->size + FF_INPUT_BUFFER_PADDING_SIZE) < buf->max_size) && + (buf->decoder_flags & BUF_FLAG_FRAME_END)) { + /* buf contains a complete frame */ + /* no memcpy needed */ + chunk_buf = buf->content; + this->size = buf->size; + lprintf("no memcpy needed to accumulate data\n"); + } else { + /* copy data into our internal buffer */ + ff_check_bufsize(this, this->size + buf->size); + chunk_buf = this->buf; /* ff_check_bufsize might realloc this->buf */ + + xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size); + + this->size += buf->size; + lprintf("accumulate data into this->buf\n"); + } + } + + if (buf->decoder_flags & BUF_FLAG_FRAME_END) { + + vo_frame_t *img; + int free_img; + int got_picture, len; + int got_one_picture = 0; + int offset = 0; + int codec_type = buf->type & 0xFFFF0000; + + /* pad input data */ + /* note: bitstream, alt bitstream reader or something will cause + * severe mpeg4 artifacts if padding is less than 32 bits. + */ + memset(&chunk_buf[this->size], 0, FF_INPUT_BUFFER_PADDING_SIZE); + + while (this->size > 0) { + + /* DV frames can be completely skipped */ + if( codec_type == BUF_VIDEO_DV && this->skipframes ) { + this->size = 0; + got_picture = 0; + } else { + /* skip decoding b frames if too late */ + this->context->hurry_up = (this->skipframes > 0); + + lprintf("buffer size: %d\n", this->size); + len = avcodec_decode_video (this->context, this->av_frame, + &got_picture, &chunk_buf[offset], + this->size); + lprintf("consumed size: %d, got_picture: %d\n", len, got_picture); + if ((len <= 0) || (len > this->size)) { + xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, + "ffmpeg_video_dec: error decompressing frame\n"); + this->size = 0; + + } else { + + offset += len; + this->size -= len; + + if (this->size > 0) { + ff_check_bufsize(this, this->size); + memmove (this->buf, &chunk_buf[offset], this->size); + chunk_buf = this->buf; + } + } + } + + /* aspect ratio provided by ffmpeg, override previous setting */ + if ((this->aspect_ratio_prio < 2) && + av_cmp_q(this->context->sample_aspect_ratio, avr00)) { + + this->aspect_ratio = av_q2d(this->context->sample_aspect_ratio) * + (double)this->bih.biWidth / (double)this->bih.biHeight; + this->aspect_ratio_prio = 2; + lprintf("ffmpeg aspect ratio: %f\n", this->aspect_ratio); + set_stream_info(this); + } + + if (got_picture && this->av_frame->data[0]) { + /* got a picture, draw it */ + got_one_picture = 1; + if(!this->av_frame->opaque) { + /* indirect rendering */ + + /* initialize the colorspace converter */ + if (!this->cs_convert_init) { + if ((this->context->pix_fmt == PIX_FMT_RGBA32) || + (this->context->pix_fmt == PIX_FMT_RGB565) || + (this->context->pix_fmt == PIX_FMT_RGB555) || + (this->context->pix_fmt == PIX_FMT_BGR24) || + (this->context->pix_fmt == PIX_FMT_RGB24) || + (this->context->pix_fmt == PIX_FMT_PAL8)) { + this->output_format = XINE_IMGFMT_YUY2; + init_yuv_planes(&this->yuv, this->bih.biWidth, this->bih.biHeight); + this->yuv_init = 1; + } + this->cs_convert_init = 1; + } + + if (this->aspect_ratio_prio == 0) { + this->aspect_ratio = (double)this->bih.biWidth / (double)this->bih.biHeight; + this->aspect_ratio_prio = 1; + lprintf("default aspect ratio: %f\n", this->aspect_ratio); + set_stream_info(this); + } + + img = this->stream->video_out->get_frame (this->stream->video_out, + this->bih.biWidth, + this->bih.biHeight, + this->aspect_ratio, + this->output_format, + VO_BOTH_FIELDS|this->frame_flags); + free_img = 1; + } else { + /* DR1 */ + img = (vo_frame_t*) this->av_frame->opaque; + free_img = 0; + } + + /* post processing */ + if(this->pp_quality != this->class->pp_quality) + pp_change_quality(this); + + if(this->pp_available && this->pp_quality) { + + if(this->av_frame->opaque) { + /* DR1 */ + img = this->stream->video_out->get_frame (this->stream->video_out, + img->width, + img->height, + this->aspect_ratio, + this->output_format, + VO_BOTH_FIELDS|this->frame_flags); + free_img = 1; + } + + pp_postprocess(this->av_frame->data, this->av_frame->linesize, + img->base, img->pitches, + img->width, img->height, + this->av_frame->qscale_table, this->av_frame->qstride, + this->pp_mode, this->pp_context, + this->av_frame->pict_type); + + } else if (!this->av_frame->opaque) { + /* colorspace conversion or copy */ + ff_convert_frame(this, img); + } + + img->pts = this->pts; + this->pts = 0; + + /* workaround for weird 120fps streams */ + if( this->video_step == 750 ) { + /* fallback to the VIDEO_PTS_MODE */ + this->video_step = 0; + } + + if (this->av_frame->repeat_pict) + img->duration = this->video_step * 3 / 2; + else + img->duration = this->video_step; + + img->crop_right = this->crop_right; + img->crop_bottom = this->crop_bottom; + + this->skipframes = img->draw(img, this->stream); + + if(free_img) + img->free(img); + } + } + + if (!got_one_picture) { + /* skipped frame, output a bad frame */ + img = this->stream->video_out->get_frame (this->stream->video_out, + this->bih.biWidth, + this->bih.biHeight, + this->aspect_ratio, + this->output_format, + VO_BOTH_FIELDS|this->frame_flags); + img->pts = 0; + img->duration = this->video_step; + img->bad_frame = 1; + this->skipframes = img->draw(img, this->stream); + img->free(img); + } + } +} + +static void ff_decode_data (video_decoder_t *this_gen, buf_element_t *buf) { + ff_video_decoder_t *this = (ff_video_decoder_t *) this_gen; + + lprintf ("processing packet type = %08x, len = %d, decoder_flags=%08x\n", + buf->type, buf->size, buf->decoder_flags); + + if (buf->decoder_flags & BUF_FLAG_FRAMERATE) { + this->video_step = buf->decoder_info[0]; + _x_stream_info_set(this->stream, XINE_STREAM_INFO_FRAME_DURATION, this->video_step); + } + + if (buf->decoder_flags & BUF_FLAG_PREVIEW) { + + ff_handle_preview_buffer(this, buf); + + } else { + + if (buf->decoder_flags & BUF_FLAG_SPECIAL) { + + ff_handle_special_buffer(this, buf); + + } + + if (buf->decoder_flags & BUF_FLAG_HEADER) { + + ff_handle_header_buffer(this, buf); + + if (buf->decoder_flags & BUF_FLAG_ASPECT) { + if (this->aspect_ratio_prio < 3) { + this->aspect_ratio = (double)buf->decoder_info[1] / (double)buf->decoder_info[2]; + this->aspect_ratio_prio = 3; + lprintf("aspect ratio: %f\n", this->aspect_ratio); + set_stream_info(this); + } + } + + } else { + + /* decode */ + if (buf->pts) + this->pts = buf->pts; + + if (this->is_mpeg12) { + ff_handle_mpeg12_buffer(this, buf); + } else { + ff_handle_buffer(this, buf); + } + + } + } +} + +static void ff_flush (video_decoder_t *this_gen) { + lprintf ("ff_flush\n"); +} + +static void ff_reset (video_decoder_t *this_gen) { + ff_video_decoder_t *this = (ff_video_decoder_t *) this_gen; + + lprintf ("ff_reset\n"); + + this->size = 0; + + if(this->context && this->decoder_ok) + avcodec_flush_buffers(this->context); + + if (this->is_mpeg12) + mpeg_parser_reset(this->mpeg_parser); +} + +static void ff_discontinuity (video_decoder_t *this_gen) { + ff_video_decoder_t *this = (ff_video_decoder_t *) this_gen; + + lprintf ("ff_discontinuity\n"); + this->pts = 0; +} + +static void ff_dispose (video_decoder_t *this_gen) { + ff_video_decoder_t *this = (ff_video_decoder_t *) this_gen; + + lprintf ("ff_dispose\n"); + + if (this->decoder_ok) { + xine_list_iterator_t it; + AVFrame *av_frame; + + pthread_mutex_lock(&ffmpeg_lock); + avcodec_close (this->context); + pthread_mutex_unlock(&ffmpeg_lock); + + /* frame garbage collector here - workaround for buggy ffmpeg codecs that + * don't release their DR1 frames */ + while( (it = xine_list_front(this->dr1_frames)) != NULL ) + { + av_frame = (AVFrame *)xine_list_get_value(this->dr1_frames, it); + release_buffer(this->context, av_frame); + } + + this->stream->video_out->close(this->stream->video_out, this->stream); + this->decoder_ok = 0; + } + + if(this->context && this->context->slice_offset) + free(this->context->slice_offset); + + if(this->context && this->context->extradata) + free(this->context->extradata); + + if(this->yuv_init) + free_yuv_planes(&this->yuv); + + if( this->context ) + free( this->context ); + + if( this->av_frame ) + free( this->av_frame ); + + if (this->buf) + free(this->buf); + this->buf = NULL; + + if(this->pp_context) + pp_free_context(this->pp_context); + + if(this->pp_mode) + pp_free_mode(this->pp_mode); + + mpeg_parser_dispose(this->mpeg_parser); + + xine_list_delete(this->dr1_frames); + + free (this_gen); +} + +static video_decoder_t *ff_video_open_plugin (video_decoder_class_t *class_gen, xine_stream_t *stream) { + + ff_video_decoder_t *this ; + + lprintf ("open_plugin\n"); + + this = (ff_video_decoder_t *) xine_xmalloc (sizeof (ff_video_decoder_t)); + + this->video_decoder.decode_data = ff_decode_data; + this->video_decoder.flush = ff_flush; + this->video_decoder.reset = ff_reset; + this->video_decoder.discontinuity = ff_discontinuity; + this->video_decoder.dispose = ff_dispose; + this->size = 0; + + this->stream = stream; + this->class = (ff_video_class_t *) class_gen; + + this->av_frame = avcodec_alloc_frame(); + this->context = avcodec_alloc_context(); + this->context->opaque = this; + this->context->palctrl = NULL; + + this->decoder_ok = 0; + this->decoder_init_mode = 1; + this->buf = xine_xmalloc(VIDEOBUFSIZE + FF_INPUT_BUFFER_PADDING_SIZE); + this->bufsize = VIDEOBUFSIZE; + + this->is_mpeg12 = 0; + this->aspect_ratio = 0; + + this->pp_quality = 0; + this->pp_context = NULL; + this->pp_mode = NULL; + + this->mpeg_parser = NULL; + + this->dr1_frames = xine_list_new(); + + return &this->video_decoder; +} + +static char *ff_video_get_identifier (video_decoder_class_t *this) { + return "ffmpeg video"; +} + +static char *ff_video_get_description (video_decoder_class_t *this) { + return "ffmpeg based video decoder plugin"; +} + +static void ff_video_dispose_class (video_decoder_class_t *this) { + free (this); +} + +void *init_video_plugin (xine_t *xine, void *data) { + + ff_video_class_t *this; + config_values_t *config; + + this = (ff_video_class_t *) xine_xmalloc (sizeof (ff_video_class_t)); + + this->decoder_class.open_plugin = ff_video_open_plugin; + this->decoder_class.get_identifier = ff_video_get_identifier; + this->decoder_class.get_description = ff_video_get_description; + this->decoder_class.dispose = ff_video_dispose_class; + this->xine = xine; + + pthread_once( &once_control, init_once_routine ); + + /* Configuration for post processing quality - default to mid (3) for the + * moment */ + config = xine->config; + + this->pp_quality = xine->config->register_range(config, "video.processing.ffmpeg_pp_quality", 3, + 0, PP_QUALITY_MAX, + _("MPEG-4 postprocessing quality"), + _("You can adjust the amount of post processing applied to MPEG-4 video.\n" + "Higher values result in better quality, but need more CPU. Lower values may " + "result in image defects like block artifacts. For high quality content, " + "too heavy post processing can actually make the image worse by blurring it " + "too much."), + 10, pp_quality_cb, this); + + return this; +} + +static uint32_t supported_video_types[] = { + #ifdef CONFIG_MSMPEG4V1_DECODER + BUF_VIDEO_MSMPEG4_V1, + #endif + #ifdef CONFIG_MSMPEG4V2_DECODER + BUF_VIDEO_MSMPEG4_V2, + #endif + #ifdef CONFIG_MSMPEG4V3_DECODER + BUF_VIDEO_MSMPEG4_V3, + #endif + #ifdef CONFIG_WMV1_DECODER + BUF_VIDEO_WMV7, + #endif + #ifdef CONFIG_WMV2_DECODER + BUF_VIDEO_WMV8, + #endif + #ifdef CONFIG_WMV3_DECODER + BUF_VIDEO_WMV9, + #endif + #ifdef CONFIG_MPEG4_DECODER + BUF_VIDEO_MPEG4, + #endif + #ifdef CONFIG_MPEG4_DECODER + BUF_VIDEO_XVID, + #endif + #ifdef CONFIG_MPEG4_DECODER + BUF_VIDEO_DIVX5, + #endif + #ifdef CONFIG_MPEG4_DECODER + BUF_VIDEO_3IVX, + #endif + #ifdef CONFIG_MJPEG_DECODER + BUF_VIDEO_JPEG, + #endif + #ifdef CONFIG_MJPEG_DECODER + BUF_VIDEO_MJPEG, + #endif + #ifdef CONFIG_MJPEGB_DECODER + BUF_VIDEO_MJPEG_B, + #endif + #ifdef CONFIG_H263I_DECODER + BUF_VIDEO_I263, + #endif + #ifdef CONFIG_H263_DECODER + BUF_VIDEO_H263, + #endif + #ifdef CONFIG_RV10_DECODER + BUF_VIDEO_RV10, + #endif + #ifdef CONFIG_RV20_DECODER + BUF_VIDEO_RV20, + #endif + #ifdef CONFIG_INDEO3_DECODER + BUF_VIDEO_IV31, + #endif + #ifdef CONFIG_INDEO3_DECODER + BUF_VIDEO_IV32, + #endif + #ifdef CONFIG_SVQ1_DECODER + BUF_VIDEO_SORENSON_V1, + #endif + #ifdef CONFIG_SVQ3_DECODER + BUF_VIDEO_SORENSON_V3, + #endif + #ifdef CONFIG_DVVIDEO_DECODER + BUF_VIDEO_DV, + #endif + #ifdef CONFIG_HUFFYUV_DECODER + BUF_VIDEO_HUFFYUV, + #endif + #ifdef CONFIG_VP3_DECODER + BUF_VIDEO_VP31, + #endif + #ifdef CONFIG_VP5_DECODER + BUF_VIDEO_VP5, + #endif + #ifdef CONFIG_VP6_DECODER + BUF_VIDEO_VP6, + BUF_VIDEO_VP6F, + #endif + #ifdef CONFIG_4XM_DECODER + BUF_VIDEO_4XM, + #endif + #ifdef CONFIG_CINEPAK_DECODER + BUF_VIDEO_CINEPAK, + #endif + #ifdef CONFIG_MSVIDEO1_DECODER + BUF_VIDEO_MSVC, + #endif + #ifdef CONFIG_MSRLE_DECODER + BUF_VIDEO_MSRLE, + #endif + #ifdef CONFIG_RPZA_DECODER + BUF_VIDEO_RPZA, + #endif + #ifdef CONFIG_CYUV_DECODER + BUF_VIDEO_CYUV, + #endif + #ifdef CONFIG_ROQ_DECODER + BUF_VIDEO_ROQ, + #endif + #ifdef CONFIG_IDCIN_DECODER + BUF_VIDEO_IDCIN, + #endif + #ifdef CONFIG_XAN_WC3_DECODER + BUF_VIDEO_WC3, + #endif + #ifdef CONFIG_WS_VQA_DECODER + BUF_VIDEO_VQA, + #endif + #ifdef CONFIG_INTERPLAY_VIDEO_DECODER + BUF_VIDEO_INTERPLAY, + #endif + #ifdef CONFIG_FLIC_DECODER + BUF_VIDEO_FLI, + #endif + #ifdef CONFIG_8BPS_DECODER + BUF_VIDEO_8BPS, + #endif + #ifdef CONFIG_SMC_DECODER + BUF_VIDEO_SMC, + #endif + #ifdef CONFIG_TRUEMOTION1_DECODER + BUF_VIDEO_DUCKTM1, + #endif + #ifdef CONFIG_TRUEMOTION2_DECODER + BUF_VIDEO_DUCKTM2, + #endif + #ifdef CONFIG_VMDVIDEO_DECODER + BUF_VIDEO_VMD, + #endif + #ifdef CONFIG_ZLIB_DECODER + BUF_VIDEO_ZLIB, + #endif + #ifdef CONFIG_MSZH_DECODER + BUF_VIDEO_MSZH, + #endif + #ifdef CONFIG_ASV1_DECODER + BUF_VIDEO_ASV1, + #endif + #ifdef CONFIG_ASV2_DECODER + BUF_VIDEO_ASV2, + #endif + #ifdef CONFIG_VCR1_DECODER + BUF_VIDEO_ATIVCR1, + #endif + #ifdef CONFIG_FLV_DECODER + BUF_VIDEO_FLV1, + #endif + #ifdef CONFIG_QTRLE_DECODER + BUF_VIDEO_QTRLE, + #endif + #ifdef CONFIG_H264_DECODER + BUF_VIDEO_H264, + #endif + #ifdef CONFIG_H261_DECODER + BUF_VIDEO_H261, + #endif + #ifdef CONFIG_AASC_DECODER + BUF_VIDEO_AASC, + #endif + #ifdef CONFIG_LOCO_DECODER + BUF_VIDEO_LOCO, + #endif + #ifdef CONFIG_QDRAW_DECODER + BUF_VIDEO_QDRW, + #endif + #ifdef CONFIG_QPEG_DECODER + BUF_VIDEO_QPEG, + #endif + #ifdef CONFIG_TSCC_DECODER + BUF_VIDEO_TSCC, + #endif + #ifdef CONFIG_ULTI_DECODER + BUF_VIDEO_ULTI, + #endif + #ifdef CONFIG_WNV1_DECODER + BUF_VIDEO_WNV1, + #endif + #ifdef CONFIG_VIXL_DECODER + BUF_VIDEO_XL, + #endif + #ifdef CONFIG_INDEO2_DECODER + BUF_VIDEO_RT21, + #endif + #ifdef CONFIG_FRAPS_DECODER + BUF_VIDEO_FPS1, + #endif + #ifdef CONFIG_MPEG1VIDEO_DECODER + BUF_VIDEO_MPEG, + #endif + #ifdef CONFIG_CSCD_DECODER + BUF_VIDEO_CSCD, + #endif + #ifdef CONFIG_AVS_DECODER + BUF_VIDEO_AVS, + #endif + #ifdef CONFIG_MMVIDEO_DECODER + BUF_VIDEO_ALGMM, + #endif + #ifdef CONFIG_ZMBV_DECODER + BUF_VIDEO_ZMBV, + #endif + #ifdef CONFIG_SMACKVIDEO_DECODER + BUF_VIDEO_SMACKER, + #endif + #ifdef CONFIG_NUV_DECODER + BUF_VIDEO_NUV, + #endif + #ifdef CONFIG_KMVC_DECODER + BUF_VIDEO_KMVC, + #endif + #ifdef CONFIG_FLASHSV_DECODER + BUF_VIDEO_FLASHSV, + #endif + #ifdef CONFIG_CAVS_DECODER + BUF_VIDEO_CAVS, + #endif + + 0 +}; + +static uint32_t wmv8_video_types[] = { + BUF_VIDEO_WMV8, + 0 +}; + +static uint32_t wmv9_video_types[] = { + BUF_VIDEO_WMV9, + 0 +}; + +decoder_info_t dec_info_ffmpeg_video = { + supported_video_types, /* supported types */ + 6 /* priority */ +}; + +decoder_info_t dec_info_ffmpeg_wmv8 = { + wmv8_video_types, /* supported types */ + 0 /* priority */ +}; + +decoder_info_t dec_info_ffmpeg_wmv9 = { + wmv9_video_types, /* supported types */ + 0 /* priority */ +}; diff --git a/src/libffmpeg/ffmpeg_decoder.c b/src/libffmpeg/ffmpeg_decoder.c new file mode 100644 index 000000000..6f85cc1fa --- /dev/null +++ b/src/libffmpeg/ffmpeg_decoder.c @@ -0,0 +1,329 @@ +/* + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * $Id: xine_decoder.c,v 1.173 2007/01/13 21:19:52 miguelfreitas Exp $ + * + * xine decoder plugin using ffmpeg + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#include "ffmpeg_config.h" +#endif + +#include "xine_internal.h" + +#include "ffmpeg_decoder.h" + +/* + * common initialisation + */ + +pthread_once_t once_control = PTHREAD_ONCE_INIT; +pthread_mutex_t ffmpeg_lock; + +#ifndef HAVE_FFMPEG + +#define REGISTER_ENCODER(X,x) \ + if(ENABLE_##X##_ENCODER) register_avcodec(&x##_encoder) +#define REGISTER_DECODER(X,x) \ + if(ENABLE_##X##_DECODER) register_avcodec(&x##_decoder) +#define REGISTER_ENCDEC(X,x) REGISTER_ENCODER(X,x); REGISTER_DECODER(X,x) + +#define REGISTER_PARSER(X,x) \ + if(ENABLE_##X##_PARSER) av_register_codec_parser(&x##_parser) + +/* If you do not call this function, then you can select exactly which + formats you want to support */ + +/** + * simple call to register all the codecs. + */ +void avcodec_register_all(void) +{ + static int inited = 0; + + if (inited != 0) + return; + inited = 1; + + /* video codecs */ + REGISTER_DECODER(AASC, aasc); + REGISTER_ENCDEC (ASV1, asv1); + REGISTER_ENCDEC (ASV2, asv2); + REGISTER_DECODER(AVS, avs); + REGISTER_DECODER(BMP, bmp); + REGISTER_DECODER(CAVS, cavs); + REGISTER_DECODER(CINEPAK, cinepak); + REGISTER_DECODER(CLJR, cljr); + REGISTER_DECODER(CSCD, cscd); + REGISTER_DECODER(CYUV, cyuv); + REGISTER_DECODER(DSICINVIDEO, dsicinvideo); + REGISTER_ENCDEC (DVVIDEO, dvvideo); + REGISTER_DECODER(EIGHTBPS, eightbps); + REGISTER_ENCDEC (FFV1, ffv1); + REGISTER_ENCDEC (FFVHUFF, ffvhuff); + REGISTER_DECODER(FLASHSV, flashsv); + REGISTER_DECODER(FLIC, flic); + REGISTER_ENCDEC (FLV, flv); + REGISTER_DECODER(FOURXM, fourxm); + REGISTER_DECODER(FRAPS, fraps); + REGISTER_ENCDEC (GIF, gif); + REGISTER_ENCDEC (H261, h261); + REGISTER_ENCDEC (H263, h263); + REGISTER_DECODER(H263I, h263i); + REGISTER_ENCODER(H263P, h263p); + REGISTER_DECODER(H264, h264); + REGISTER_ENCDEC (HUFFYUV, huffyuv); + REGISTER_DECODER(IDCIN, idcin); + REGISTER_DECODER(INDEO2, indeo2); + REGISTER_DECODER(INDEO3, indeo3); + REGISTER_DECODER(INTERPLAY_VIDEO, interplay_video); + REGISTER_ENCODER(JPEGLS, jpegls); + REGISTER_DECODER(KMVC, kmvc); + REGISTER_ENCODER(LJPEG, ljpeg); + REGISTER_DECODER(LOCO, loco); + REGISTER_DECODER(MDEC, mdec); + REGISTER_ENCDEC (MJPEG, mjpeg); + REGISTER_DECODER(MJPEGB, mjpegb); + REGISTER_DECODER(MMVIDEO, mmvideo); +#ifdef HAVE_XVMC + REGISTER_DECODER(MPEG_XVMC, mpeg_xvmc); +#endif + REGISTER_ENCDEC (MPEG1VIDEO, mpeg1video); + REGISTER_ENCDEC (MPEG2VIDEO, mpeg2video); + REGISTER_ENCDEC (MPEG4, mpeg4); + REGISTER_DECODER(MPEGVIDEO, mpegvideo); + REGISTER_ENCDEC (MSMPEG4V1, msmpeg4v1); + REGISTER_ENCDEC (MSMPEG4V2, msmpeg4v2); + REGISTER_ENCDEC (MSMPEG4V3, msmpeg4v3); + REGISTER_DECODER(MSRLE, msrle); + REGISTER_DECODER(MSVIDEO1, msvideo1); + REGISTER_DECODER(MSZH, mszh); + REGISTER_DECODER(NUV, nuv); + REGISTER_ENCODER(PAM, pam); + REGISTER_ENCODER(PBM, pbm); + REGISTER_ENCODER(PGM, pgm); + REGISTER_ENCODER(PGMYUV, pgmyuv); +#ifdef CONFIG_ZLIB + REGISTER_ENCDEC (PNG, png); +#endif + REGISTER_ENCODER(PPM, ppm); + REGISTER_DECODER(QDRAW, qdraw); + REGISTER_DECODER(QPEG, qpeg); + REGISTER_DECODER(QTRLE, qtrle); + REGISTER_ENCDEC (RAWVIDEO, rawvideo); + REGISTER_DECODER(ROQ, roq); + REGISTER_DECODER(RPZA, rpza); + REGISTER_ENCDEC (RV10, rv10); + REGISTER_ENCDEC (RV20, rv20); + REGISTER_DECODER(SMACKER, smacker); + REGISTER_DECODER(SMC, smc); + REGISTER_ENCDEC (SNOW, snow); + REGISTER_DECODER(SP5X, sp5x); + REGISTER_ENCDEC (SVQ1, svq1); + REGISTER_DECODER(SVQ3, svq3); + REGISTER_DECODER(TARGA, targa); + REGISTER_DECODER(THEORA, theora); + REGISTER_DECODER(TIERTEXSEQVIDEO, tiertexseqvideo); + REGISTER_DECODER(TIFF, tiff); + REGISTER_DECODER(TRUEMOTION1, truemotion1); + REGISTER_DECODER(TRUEMOTION2, truemotion2); + REGISTER_DECODER(TSCC, tscc); + REGISTER_DECODER(ULTI, ulti); + REGISTER_DECODER(VC1, vc1); + REGISTER_DECODER(VCR1, vcr1); + REGISTER_DECODER(VMDVIDEO, vmdvideo); + REGISTER_DECODER(VMNC, vmnc); + REGISTER_DECODER(VP3, vp3); + REGISTER_DECODER(VP5, vp5); + REGISTER_DECODER(VP6, vp6); + REGISTER_DECODER(VP6F, vp6f); + REGISTER_DECODER(VQA, vqa); + REGISTER_ENCDEC (WMV1, wmv1); + REGISTER_ENCDEC (WMV2, wmv2); + REGISTER_DECODER(WMV3, wmv3); + REGISTER_DECODER(WNV1, wnv1); +#ifdef CONFIG_X264 + REGISTER_ENCODER(X264, x264); +#endif + REGISTER_DECODER(XAN_WC3, xan_wc3); + REGISTER_DECODER(XL, xl); +#ifdef CONFIG_XVID + REGISTER_ENCODER(XVID, xvid); +#endif + REGISTER_ENCDEC (ZLIB, zlib); +#ifdef CONFIG_ZLIB + REGISTER_ENCDEC (ZMBV, zmbv); +#endif + + /* audio codecs */ +#ifdef CONFIG_LIBFAAD + REGISTER_DECODER(AAC, aac); + REGISTER_DECODER(MPEG4AAC, mpeg4aac); +#endif +#ifdef CONFIG_LIBA52 + REGISTER_DECODER(AC3, ac3); +#endif + REGISTER_ENCODER(AC3, ac3); + REGISTER_DECODER(ALAC, alac); +#if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED) + REGISTER_ENCDEC (AMR_NB, amr_nb); +#endif +#ifdef CONFIG_AMR_WB + REGISTER_ENCDEC (AMR_WB, amr_wb); +#endif + REGISTER_DECODER(COOK, cook); + REGISTER_DECODER(DSICINAUDIO, dsicinaudio); +#ifdef CONFIG_LIBDTS + REGISTER_DECODER(DTS, dts); +#endif +#ifdef CONFIG_LIBFAAC + REGISTER_ENCODER(FAAC, faac); +#endif + REGISTER_ENCDEC (FLAC, flac); + REGISTER_DECODER(IMC, imc); +#ifdef CONFIG_LIBGSM + REGISTER_ENCDEC (LIBGSM, libgsm); +#endif + REGISTER_DECODER(MACE3, mace3); + REGISTER_DECODER(MACE6, mace6); + REGISTER_ENCDEC (MP2, mp2); + REGISTER_DECODER(MP3, mp3); + REGISTER_DECODER(MP3ADU, mp3adu); +#ifdef CONFIG_LIBMP3LAME + REGISTER_ENCODER(MP3LAME, mp3lame); +#endif + REGISTER_DECODER(MP3ON4, mp3on4); + REGISTER_DECODER(MPC7, mpc7); +#ifdef CONFIG_LIBVORBIS + if (!ENABLE_VORBIS_ENCODER) REGISTER_ENCODER(OGGVORBIS, oggvorbis); + if (!ENABLE_VORBIS_DECODER) REGISTER_DECODER(OGGVORBIS, oggvorbis); +#endif + REGISTER_DECODER(QDM2, qdm2); + REGISTER_DECODER(RA_144, ra_144); + REGISTER_DECODER(RA_288, ra_288); + REGISTER_DECODER(SHORTEN, shorten); + REGISTER_DECODER(SMACKAUD, smackaud); + REGISTER_ENCDEC (SONIC, sonic); + REGISTER_ENCODER(SONIC_LS, sonic_ls); + REGISTER_DECODER(TRUESPEECH, truespeech); + REGISTER_DECODER(TTA, tta); + REGISTER_DECODER(VMDAUDIO, vmdaudio); + REGISTER_ENCDEC (VORBIS, vorbis); + REGISTER_DECODER(WAVPACK, wavpack); + REGISTER_DECODER(WMAV1, wmav1); + REGISTER_DECODER(WMAV2, wmav2); + REGISTER_DECODER(WS_SND1, ws_snd1); + + /* pcm codecs */ + REGISTER_ENCDEC (PCM_ALAW, pcm_alaw); + REGISTER_ENCDEC (PCM_MULAW, pcm_mulaw); + REGISTER_ENCDEC (PCM_S8, pcm_s8); + REGISTER_ENCDEC (PCM_S16BE, pcm_s16be); + REGISTER_ENCDEC (PCM_S16LE, pcm_s16le); + REGISTER_ENCDEC (PCM_S24BE, pcm_s24be); + REGISTER_ENCDEC (PCM_S24DAUD, pcm_s24daud); + REGISTER_ENCDEC (PCM_S24LE, pcm_s24le); + REGISTER_ENCDEC (PCM_S32BE, pcm_s32be); + REGISTER_ENCDEC (PCM_S32LE, pcm_s32le); + REGISTER_ENCDEC (PCM_U8, pcm_u8); + REGISTER_ENCDEC (PCM_U16BE, pcm_u16be); + REGISTER_ENCDEC (PCM_U16LE, pcm_u16le); + REGISTER_ENCDEC (PCM_U24BE, pcm_u24be); + REGISTER_ENCDEC (PCM_U24LE, pcm_u24le); + REGISTER_ENCDEC (PCM_U32BE, pcm_u32be); + REGISTER_ENCDEC (PCM_U32LE, pcm_u32le); + + /* dpcm codecs */ + REGISTER_DECODER(INTERPLAY_DPCM, interplay_dpcm); + REGISTER_DECODER(ROQ_DPCM, roq_dpcm); + REGISTER_DECODER(SOL_DPCM, sol_dpcm); + REGISTER_DECODER(XAN_DPCM, xan_dpcm); + + /* adpcm codecs */ + REGISTER_ENCDEC (ADPCM_4XM, adpcm_4xm); + REGISTER_ENCDEC (ADPCM_ADX, adpcm_adx); + REGISTER_ENCDEC (ADPCM_CT, adpcm_ct); + REGISTER_ENCDEC (ADPCM_EA, adpcm_ea); + REGISTER_ENCDEC (ADPCM_G726, adpcm_g726); + REGISTER_ENCDEC (ADPCM_IMA_DK3, adpcm_ima_dk3); + REGISTER_ENCDEC (ADPCM_IMA_DK4, adpcm_ima_dk4); + REGISTER_ENCDEC (ADPCM_IMA_QT, adpcm_ima_qt); + REGISTER_ENCDEC (ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg); + REGISTER_ENCDEC (ADPCM_IMA_WAV, adpcm_ima_wav); + REGISTER_ENCDEC (ADPCM_IMA_WS, adpcm_ima_ws); + REGISTER_ENCDEC (ADPCM_MS, adpcm_ms); + REGISTER_ENCDEC (ADPCM_SBPRO_2, adpcm_sbpro_2); + REGISTER_ENCDEC (ADPCM_SBPRO_3, adpcm_sbpro_3); + REGISTER_ENCDEC (ADPCM_SBPRO_4, adpcm_sbpro_4); + REGISTER_ENCDEC (ADPCM_SWF, adpcm_swf); + REGISTER_ENCDEC (ADPCM_XA, adpcm_xa); + REGISTER_ENCDEC (ADPCM_YAMAHA, adpcm_yamaha); + + /* subtitles */ + REGISTER_ENCDEC (DVBSUB, dvbsub); + REGISTER_ENCDEC (DVDSUB, dvdsub); + + /* parsers */ + REGISTER_PARSER (AAC, aac); + REGISTER_PARSER (AC3, ac3); + REGISTER_PARSER (CAVSVIDEO, cavsvideo); + REGISTER_PARSER (DVBSUB, dvbsub); + REGISTER_PARSER (DVDSUB, dvdsub); + REGISTER_PARSER (H261, h261); + REGISTER_PARSER (H263, h263); + REGISTER_PARSER (H264, h264); + REGISTER_PARSER (MJPEG, mjpeg); + REGISTER_PARSER (MPEG4VIDEO, mpeg4video); + REGISTER_PARSER (MPEGAUDIO, mpegaudio); + REGISTER_PARSER (MPEGVIDEO, mpegvideo); + REGISTER_PARSER (PNM, pnm); + + /* + av_register_bitstream_filter(&dump_extradata_bsf); + av_register_bitstream_filter(&remove_extradata_bsf); + av_register_bitstream_filter(&noise_bsf); + av_register_bitstream_filter(&mp3_header_compress_bsf); + av_register_bitstream_filter(&mp3_header_decompress_bsf); + av_register_bitstream_filter(&mjpega_dump_header_bsf); + */ +} + +#endif + +void init_once_routine(void) { + pthread_mutex_init(&ffmpeg_lock, NULL); + avcodec_init(); + avcodec_register_all(); +} + +/* + * exported plugin catalog entry + */ + +const plugin_info_t xine_plugin_info[] EXPORTED = { + /* type, API, "name", version, special_info, init_function */ + { PLUGIN_VIDEO_DECODER | PLUGIN_MUST_PRELOAD, 18, "ffmpegvideo", XINE_VERSION_CODE, &dec_info_ffmpeg_video, init_video_plugin }, + { PLUGIN_VIDEO_DECODER, 18, "ffmpeg-wmv8", XINE_VERSION_CODE, &dec_info_ffmpeg_wmv8, init_video_plugin }, + { PLUGIN_VIDEO_DECODER, 18, "ffmpeg-wmv9", XINE_VERSION_CODE, &dec_info_ffmpeg_wmv9, init_video_plugin }, + { PLUGIN_AUDIO_DECODER, 15, "ffmpegaudio", XINE_VERSION_CODE, &dec_info_ffmpeg_audio, init_audio_plugin }, + { PLUGIN_NONE, 0, "", 0, NULL, NULL } +}; diff --git a/src/libffmpeg/ffmpeg_decoder.h b/src/libffmpeg/ffmpeg_decoder.h new file mode 100644 index 000000000..879ee3175 --- /dev/null +++ b/src/libffmpeg/ffmpeg_decoder.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2001-2005 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.h,v 1.7 2006/08/02 07:15:27 tmmm Exp $ + * + */ + +#ifndef HAVE_XINE_DECODER_H +#define HAVE_XINE_DECODER_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_FFMPEG +# include +#else +# include "libavcodec/avcodec.h" +#endif + +typedef struct ff_codec_s { + uint32_t type; + enum CodecID id; + const char *name; +} ff_codec_t; + +void *init_audio_plugin (xine_t *xine, void *data); +void *init_video_plugin (xine_t *xine, void *data); + +extern decoder_info_t dec_info_ffmpeg_video; +extern decoder_info_t dec_info_ffmpeg_wmv8; +extern decoder_info_t dec_info_ffmpeg_wmv9; +extern decoder_info_t dec_info_ffmpeg_audio; + +extern pthread_once_t once_control; +void init_once_routine(void); + +extern pthread_mutex_t ffmpeg_lock; + +#endif diff --git a/src/libffmpeg/ffmpeg_encoder.c b/src/libffmpeg/ffmpeg_encoder.c new file mode 100644 index 000000000..e234b4ddc --- /dev/null +++ b/src/libffmpeg/ffmpeg_encoder.c @@ -0,0 +1,334 @@ +/* + * Copyright (C) 2000-2004 the xine project + * + * This file is part of xine, a unix 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_encoder.c,v 1.25 2006/07/10 22:08:29 dgp85 Exp $ + */ + +/* mpeg encoders for the dxr3 video out plugin. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include + +#define LOG_MODULE "dxr3_mpeg_encoder" +/* #define LOG_VERBOSE */ +/* #define LOG */ + +#include "video_out_dxr3.h" + +#ifdef HAVE_FFMPEG +# include +#else +# include "libavcodec/avcodec.h" +#endif + +/* buffer size for encoded mpeg1 stream; will hold one intra frame + * at 640x480 typical sizes are <50 kB. 512 kB should be plenty */ +#define DEFAULT_BUFFER_SIZE 512*1024 + + +/*initialisation function, used by the dxr3 plugin */ +int dxr3_encoder_init(dxr3_driver_t *drv) EXPORTED; + +/* functions required by encoder api */ +static int lavc_on_update_format(dxr3_driver_t *drv, dxr3_frame_t *frame); +static int lavc_on_display_frame(dxr3_driver_t *drv, dxr3_frame_t *frame); +static int lavc_on_unneeded(dxr3_driver_t *drv); + +/*encoder structure*/ +typedef struct lavc_data_s { + encoder_data_t encoder_data; + AVCodecContext *context; /* handle for encoding */ + int width, height; /* width and height of the video frame */ + uint8_t *ffmpeg_buffer; /* lavc buffer */ + AVFrame *picture; /* picture to be encoded */ + uint8_t *out[3]; /* aligned buffer for YV12 data */ + uint8_t *buf; /* unaligned YV12 buffer */ +} lavc_data_t; + + +int dxr3_encoder_init(dxr3_driver_t *drv) +{ + lavc_data_t* this; + avcodec_init(); + + register_avcodec(&mpeg1video_encoder); + lprintf("lavc init , version %x\n", avcodec_version()); + this = xine_xmalloc(sizeof(lavc_data_t)); + if (!this) return 0; + + this->encoder_data.type = ENC_LAVC; + this->encoder_data.on_update_format = lavc_on_update_format; + this->encoder_data.on_frame_copy = NULL; + this->encoder_data.on_display_frame = lavc_on_display_frame; + this->encoder_data.on_unneeded = lavc_on_unneeded; + this->context = 0; + + drv->enc = &this->encoder_data; + return 1; +} + +/* helper function */ +static int lavc_prepare_frame(lavc_data_t *this, dxr3_driver_t *drv, dxr3_frame_t *frame); + +static int lavc_on_update_format(dxr3_driver_t *drv, dxr3_frame_t *frame) +{ + lavc_data_t *this = (lavc_data_t *)drv->enc; + AVCodec *codec; + unsigned char use_quantizer; + + if (this->context) { + avcodec_close(this->context); + free(this->context); + free(this->picture); + this->context = NULL; + this->picture = NULL; + } + + /* if YUY2 and dimensions changed, we need to re-allocate the + * internal YV12 buffer */ + if (frame->vo_frame.format == XINE_IMGFMT_YUY2) { + int image_size = frame->vo_frame.pitches[0] * frame->oheight; + + this->out[0] = xine_xmalloc_aligned(16, image_size * 3/2, + (void *)&this->buf); + this->out[1] = this->out[0] + image_size; + this->out[2] = this->out[1] + image_size/4; + + /* fill with black (yuv 16,128,128) */ + memset(this->out[0], 16, image_size); + memset(this->out[1], 128, image_size/4); + memset(this->out[2], 128, image_size/4); + lprintf("Using YUY2->YV12 conversion\n"); + } + + /* resolution must be a multiple of two */ + if ((frame->vo_frame.pitches[0] % 2 != 0) || (frame->oheight % 2 != 0)) { + xprintf(drv->class->xine, XINE_VERBOSITY_LOG, + "dxr3_mpeg_encoder: lavc only handles video dimensions which are multiples of 2\n"); + return 0; + } + + /* get mpeg codec handle */ + codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO); + if (!codec) { + xprintf(drv->class->xine, XINE_VERBOSITY_LOG, + "dxr3_mpeg_encoder: lavc MPEG1 codec not found\n"); + return 0; + } + lprintf("lavc MPEG1 encoder found.\n"); + + this->width = frame->vo_frame.pitches[0]; + this->height = frame->oheight; + + this->context = avcodec_alloc_context(); + if (!this->context) { + xprintf(drv->class->xine, XINE_VERBOSITY_LOG, + "dxr3_mpeg_encoder: Couldn't start the ffmpeg library\n"); + return 0; + } + this->picture = avcodec_alloc_frame(); + if (!this->picture) { + xprintf(drv->class->xine, XINE_VERBOSITY_LOG, + "dxr3_mpeg_encoder: Couldn't allocate ffmpeg frame\n"); + return 0; + } + + /* mpeg1 encoder only support YUV420P */ + this->context->pix_fmt = PIX_FMT_YUVJ420P; + + /* put sample parameters */ + this->context->bit_rate = drv->class->xine->config->register_range(drv->class->xine->config, + "dxr3.encoding.lavc_bitrate", 10000, 1000, 20000, + _("libavcodec mpeg output bitrate (kbit/s)"), + _("The bitrate the libavcodec mpeg encoder should use for DXR3's encoding mode. " + "Higher values will increase quality and CPU usage.\n" + "This setting is only considered, when constant quality mode is disabled."), 10, NULL, NULL); + this->context->bit_rate *= 1000; /* config in kbit/s, libavcodec wants bit/s */ + + use_quantizer = drv->class->xine->config->register_bool(drv->class->xine->config, + "dxr3.encoding.lavc_quantizer", 1, + _("constant quality mode"), + _("When enabled, libavcodec will use a constant quality mode by dynamically " + "compressing the images based on their complexity. When disabled, libavcodec " + "will use constant bitrate mode."), 10, NULL, NULL); + + if (use_quantizer) { + this->context->qmin = drv->class->xine->config->register_range(drv->class->xine->config, + "dxr3.encoding.lavc_qmin", 1, 1, 10, + _("minimum compression"), + _("The minimum compression to apply to an image in constant quality mode."), + 10, NULL, NULL); + + this->context->qmax = drv->class->xine->config->register_range(drv->class->xine->config, + "dxr3.encoding.lavc_qmax", 2, 1, 20, + _("maximum quantizer"), + _("The maximum compression to apply to an image in constant quality mode."), + 10, NULL, NULL); + } + + lprintf("lavc -> bitrate %d \n", this->context->bit_rate); + + this->context->width = frame->vo_frame.pitches[0]; + this->context->height = frame->oheight; + + this->context->gop_size = 0; /*intra frames only */ + this->context->me_method = ME_ZERO; /*motion estimation type*/ + + this->context->time_base.den = 90000; + if (frame->vo_frame.duration > 90000 / 24) + this->context->time_base.num = 90000 / 24; + else if (frame->vo_frame.duration < 90000 / 60) + this->context->time_base.num = 90000 / 60; + else + this->context->time_base.num = frame->vo_frame.duration; + /* ffmpeg can complain about illegal framerates, but since this seems no + * problem for the DXR3, we just tell ffmpeg to be more lax with */ + this->context->strict_std_compliance = -1; + + /* open avcodec */ + if (avcodec_open(this->context, codec) < 0) { + xprintf(drv->class->xine, XINE_VERBOSITY_LOG, "dxr3_mpeg_encoder: could not open codec\n"); + return 0; + } + lprintf("dxr3_mpeg_encoder: lavc MPEG1 codec opened.\n"); + + if (!this->ffmpeg_buffer) + this->ffmpeg_buffer = (unsigned char *)malloc(DEFAULT_BUFFER_SIZE); /* why allocate more than needed ?! */ + if (!this->ffmpeg_buffer) { + xprintf(drv->class->xine, XINE_VERBOSITY_LOG, + "dxr3_mpeg_encoder: Couldn't allocate temp buffer for mpeg data\n"); + return 0; + } + + return 1; +} + +static int lavc_on_display_frame(dxr3_driver_t *drv, dxr3_frame_t *frame) +{ + int size; + lavc_data_t* this = (lavc_data_t *)drv->enc; + ssize_t written; + + if (frame->vo_frame.bad_frame) return 1; + /* ignore old frames */ + if ((frame->vo_frame.pitches[0] != this->context->width) || (frame->oheight != this->context->height)) { + frame->vo_frame.free(&frame->vo_frame); + lprintf("LAVC ignoring frame !!!\n"); + return 1; + } + + /* prepare frame for conversion, handles YUY2 -> YV12 conversion when necessary */ + lavc_prepare_frame(this, drv, frame); + + /* do the encoding */ + size = avcodec_encode_video(this->context, this->ffmpeg_buffer, DEFAULT_BUFFER_SIZE, this->picture); + + frame->vo_frame.free(&frame->vo_frame); + + if (size < 0) { + xprintf(drv->class->xine, XINE_VERBOSITY_LOG, + "dxr3_mpeg_encoder: encoding failed\n"); + return 0; + } + + written = write(drv->fd_video, this->ffmpeg_buffer, size); + if (written < 0) { + xprintf(drv->class->xine, XINE_VERBOSITY_LOG, + "dxr3_mpeg_encoder: video device write failed (%s)\n", strerror(errno)); + return 0; + } + if (written != size) + xprintf(drv->class->xine, XINE_VERBOSITY_LOG, + "dxr3_mpeg_encoder: Could only write %zd of %d mpeg bytes.\n", written, size); + return 1; +} + +static int lavc_on_unneeded(dxr3_driver_t *drv) +{ + lavc_data_t *this = (lavc_data_t *)drv->enc; + lprintf("flushing buffers\n"); + if (this->context) { + avcodec_close(this->context); + free(this->context); + free(this->picture); + this->context = NULL; + this->picture = NULL; + } + return 1; +} + +static int lavc_prepare_frame(lavc_data_t *this, dxr3_driver_t *drv, dxr3_frame_t *frame) +{ + int i, j, w2; + uint8_t *yuy2; + + if (frame->vo_frame.bad_frame) return 1; + + if (frame->vo_frame.format == XINE_IMGFMT_YUY2) { + /* need YUY2->YV12 conversion */ + if (!(this->out[0] && this->out[1] && this->out[2]) ) { + lprintf("Internal YV12 buffer not created.\n"); + return 0; + } + this->picture->data[0] = this->out[0] + frame->vo_frame.pitches[0] * drv->top_bar; /* y */ + this->picture->data[1] = this->out[1] + (frame->vo_frame.pitches[0] / 2) * (drv->top_bar / 2); /* u */ + this->picture->data[2] = this->out[2] + (frame->vo_frame.pitches[0] / 2) * (drv->top_bar / 2); /* v */ + yuy2 = frame->vo_frame.base[0]; + w2 = frame->vo_frame.pitches[0] / 2; + for (i = 0; i < frame->vo_frame.height; i += 2) { + for (j = 0; j < w2; j++) { + /* packed YUV 422 is: Y[i] U[i] Y[i+1] V[i] */ + *(this->picture->data[0]++) = *(yuy2++); + *(this->picture->data[1]++) = *(yuy2++); + *(this->picture->data[0]++) = *(yuy2++); + *(this->picture->data[2]++) = *(yuy2++); + } + /* down sampling */ + for (j = 0; j < w2; j++) { + /* skip every second line for U and V */ + *(this->picture->data[0]++) = *(yuy2++); + yuy2++; + *(this->picture->data[0]++) = *(yuy2++); + yuy2++; + } + } + /* reset for encoder */ + this->picture->data[0] = this->out[0]; + this->picture->data[1] = this->out[1]; + this->picture->data[2] = this->out[2]; + } + else { /* YV12 **/ + this->picture->data[0] = frame->real_base[0]; + this->picture->data[1] = frame->real_base[1]; + this->picture->data[2] = frame->real_base[2]; + } + this->picture->linesize[0] = this->context->width; + this->picture->linesize[1] = this->context->width / 2; + this->picture->linesize[2] = this->context->width / 2; + return 1; +} diff --git a/src/libffmpeg/mpeg_parser.c b/src/libffmpeg/mpeg_parser.c deleted file mode 100644 index 14a08a456..000000000 --- a/src/libffmpeg/mpeg_parser.c +++ /dev/null @@ -1,323 +0,0 @@ -/* - * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - * - * Simple MPEG-ES parser/framer by Thibaut Mattern (tmattern@noos.fr) - * based on libmpeg2 decoder. - * - * $Id: mpeg_parser.c,v 1.7 2007/03/29 18:52:45 dgp85 Exp $ - */ -#define LOG_MODULE "mpeg_parser" -#define LOG_VERBOSE -/* -#define LOG -*/ -#include "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 = xine_xmalloc(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; -} diff --git a/src/libffmpeg/mpeg_parser.h b/src/libffmpeg/mpeg_parser.h deleted file mode 100644 index 859a0fcec..000000000 --- a/src/libffmpeg/mpeg_parser.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - * - * Simple MPEG-ES parser/framer by Thibaut Mattern (tmattern@noos.fr) - * based on libmpeg2 decoder. - * - * $Id: mpeg_parser.h,v 1.5 2007/03/29 18:52:45 dgp85 Exp $ - */ -#ifndef HAVE_MPEG_PARSER_H -#define HAVE_MPEG_PARSER_H - -#include "xine_internal.h" -#include "xine_decoder.h" - -#define BUFFER_SIZE (1194 * 1024) /* libmpeg2's buffer size */ - -/* picture coding type (mpeg2 header) */ -#define I_TYPE 1 -#define P_TYPE 2 -#define B_TYPE 3 -#define D_TYPE 4 - -typedef struct mpeg_parser_s { - uint8_t *chunk_buffer; - uint8_t *chunk_ptr; - uint8_t *chunk_start; - uint32_t shift; - int buffer_size; - uint8_t code; - uint8_t picture_coding_type; - - uint8_t is_sequence_needed:1; - uint8_t is_mpeg1:1; /* public */ - uint8_t has_sequence:1; /* public */ - uint8_t in_slice:1; - - uint8_t rate_code:4; - - int aspect_ratio_info; - - /* public properties */ - uint16_t width; - uint16_t height; - int frame_duration; - double frame_aspect_ratio; - -} mpeg_parser_t; - -/* parser initialization */ -void mpeg_parser_init (mpeg_parser_t *parser); - -/* parser disposal */ -void mpeg_parser_dispose (mpeg_parser_t *parser); - -/* read a frame - * return a pointer to the first byte of the next frame - * or NULL if more bytes are needed - * *flush is set to 1 if the decoder must be flushed (needed for still menus) - */ -uint8_t *mpeg_parser_decode_data (mpeg_parser_t *parser, - uint8_t *current, uint8_t *end, - int *flush); - -/* reset the parser */ -void mpeg_parser_reset (mpeg_parser_t *parser); - -#endif /* HAVE_MPEG_PARSER_H */ diff --git a/src/libffmpeg/video_decoder.c b/src/libffmpeg/video_decoder.c deleted file mode 100644 index 38cf160c5..000000000 --- a/src/libffmpeg/video_decoder.c +++ /dev/null @@ -1,1772 +0,0 @@ -/* - * Copyright (C) 2001-2007 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: video_decoder.c,v 1.73 2007/03/29 18:41:02 dgp85 Exp $ - * - * xine video decoder plugin using ffmpeg - * - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#include "ffmpeg_config.h" -#endif - -#include -#include -#include -#include -#include -#include -#include - -#define LOG_MODULE "ffmpeg_video_dec" -#define LOG_VERBOSE -/* -#define LOG -*/ -#include "xine_internal.h" -#include "bswap.h" -#include "buffer.h" -#include "xineutils.h" -#include "xine_decoder.h" -#include "mpeg_parser.h" - -#ifdef HAVE_FFMPEG -# include -#else -# include "libavcodec/libpostproc/postprocess.h" -#endif - -#define VIDEOBUFSIZE (128*1024) -#define SLICE_BUFFER_SIZE (1194*1024) - -#define SLICE_OFFSET_SIZE 128 - -#define ENABLE_DIRECT_RENDERING - -typedef struct ff_video_decoder_s ff_video_decoder_t; - -typedef struct ff_video_class_s { - video_decoder_class_t decoder_class; - - int pp_quality; - - xine_t *xine; -} ff_video_class_t; - -struct ff_video_decoder_s { - video_decoder_t video_decoder; - - ff_video_class_t *class; - - xine_stream_t *stream; - int64_t pts; - int video_step; - - uint8_t decoder_ok:1; - uint8_t decoder_init_mode:1; - uint8_t is_mpeg12:1; - uint8_t pp_available:1; - uint8_t yuv_init:1; - uint8_t is_direct_rendering_disabled:1; - uint8_t cs_convert_init:1; - - xine_bmiheader bih; - unsigned char *buf; - int bufsize; - int size; - int skipframes; - - int slice_offset_size; - - AVFrame *av_frame; - AVCodecContext *context; - AVCodec *codec; - - int pp_quality; - int pp_flags; - pp_context_t *pp_context; - pp_mode_t *pp_mode; - - /* mpeg-es parsing */ - mpeg_parser_t *mpeg_parser; - - double aspect_ratio; - int aspect_ratio_prio; - int frame_flags; - int crop_right, crop_bottom; - - int output_format; - - xine_list_t *dr1_frames; - - yuv_planes_t yuv; - - AVPaletteControl palette_control; -}; - - -static void set_stream_info(ff_video_decoder_t *this) { - _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_WIDTH, this->bih.biWidth); - _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_HEIGHT, this->bih.biHeight); - _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_RATIO, this->aspect_ratio * 10000); -} - -#ifdef ENABLE_DIRECT_RENDERING -/* called from ffmpeg to do direct rendering method 1 */ -static int get_buffer(AVCodecContext *context, AVFrame *av_frame){ - ff_video_decoder_t *this = (ff_video_decoder_t *)context->opaque; - vo_frame_t *img; - int width = context->width; - int height = context->height; - - if (!this->bih.biWidth || !this->bih.biHeight) { - this->bih.biWidth = width; - this->bih.biHeight = height; - - if (this->aspect_ratio_prio == 0) { - this->aspect_ratio = (double)width / (double)height; - this->aspect_ratio_prio = 1; - lprintf("default aspect ratio: %f\n", this->aspect_ratio); - set_stream_info(this); - } - } - - avcodec_align_dimensions(context, &width, &height); - - if( this->context->pix_fmt != PIX_FMT_YUV420P ) { - if (!this->is_direct_rendering_disabled) { - xprintf(this->stream->xine, XINE_VERBOSITY_LOG, - _("ffmpeg_video_dec: unsupported frame format, DR1 disabled.\n")); - this->is_direct_rendering_disabled = 1; - } - - /* FIXME: why should i have to do that ? */ - av_frame->data[0]= NULL; - av_frame->data[1]= NULL; - av_frame->data[2]= NULL; - return avcodec_default_get_buffer(context, av_frame); - } - - if((width != this->bih.biWidth) || (height != this->bih.biHeight)) { - if(this->stream->video_out->get_capabilities(this->stream->video_out) & VO_CAP_CROP) { - this->crop_right = width - this->bih.biWidth; - this->crop_bottom = height - this->bih.biHeight; - } else { - if (!this->is_direct_rendering_disabled) { - xprintf(this->stream->xine, XINE_VERBOSITY_LOG, - _("ffmpeg_video_dec: unsupported frame dimensions, DR1 disabled.\n")); - this->is_direct_rendering_disabled = 1; - } - /* FIXME: why should i have to do that ? */ - av_frame->data[0]= NULL; - av_frame->data[1]= NULL; - av_frame->data[2]= NULL; - return avcodec_default_get_buffer(context, av_frame); - } - } - - img = this->stream->video_out->get_frame (this->stream->video_out, - width, - height, - this->aspect_ratio, - this->output_format, - VO_BOTH_FIELDS|this->frame_flags); - - av_frame->opaque = img; - - av_frame->data[0]= img->base[0]; - av_frame->data[1]= img->base[1]; - av_frame->data[2]= img->base[2]; - - av_frame->linesize[0] = img->pitches[0]; - av_frame->linesize[1] = img->pitches[1]; - av_frame->linesize[2] = img->pitches[2]; - - /* We should really keep track of the ages of xine frames (see - * avcodec_default_get_buffer in libavcodec/utils.c) - * For the moment tell ffmpeg that every frame is new (age = bignumber) */ - av_frame->age = 256*256*256*64; - - av_frame->type= FF_BUFFER_TYPE_USER; - - xine_list_push_back(this->dr1_frames, av_frame); - - return 0; -} - -static void release_buffer(struct AVCodecContext *context, AVFrame *av_frame){ - ff_video_decoder_t *this = (ff_video_decoder_t *)context->opaque; - - if (av_frame->type == FF_BUFFER_TYPE_USER) { - vo_frame_t *img = (vo_frame_t *)av_frame->opaque; - xine_list_iterator_t it; - - assert(av_frame->opaque); - img->free(img); - - it = xine_list_find(this->dr1_frames, av_frame); - assert(it); - if( it != NULL ) - xine_list_remove(this->dr1_frames, it); - } else { - avcodec_default_release_buffer(context, av_frame); - } - - av_frame->opaque = NULL; - av_frame->data[0]= NULL; - av_frame->data[1]= NULL; - av_frame->data[2]= NULL; -} -#endif - -static const ff_codec_t ff_video_lookup[] = { - {BUF_VIDEO_MSMPEG4_V1, CODEC_ID_MSMPEG4V1, "Microsoft MPEG-4 v1 (ffmpeg)"}, - {BUF_VIDEO_MSMPEG4_V2, CODEC_ID_MSMPEG4V2, "Microsoft MPEG-4 v2 (ffmpeg)"}, - {BUF_VIDEO_MSMPEG4_V3, CODEC_ID_MSMPEG4V3, "Microsoft MPEG-4 v3 (ffmpeg)"}, - {BUF_VIDEO_WMV7, CODEC_ID_WMV1, "MS Windows Media Video 7 (ffmpeg)"}, - {BUF_VIDEO_WMV8, CODEC_ID_WMV2, "MS Windows Media Video 8 (ffmpeg)"}, - {BUF_VIDEO_WMV9, CODEC_ID_WMV3, "MS Windows Media Video 9 (ffmpeg)"}, - {BUF_VIDEO_MPEG4, CODEC_ID_MPEG4, "ISO MPEG-4 (ffmpeg)"}, - {BUF_VIDEO_XVID, CODEC_ID_MPEG4, "ISO MPEG-4 (XviD, ffmpeg)"}, - {BUF_VIDEO_DIVX5, CODEC_ID_MPEG4, "ISO MPEG-4 (DivX5, ffmpeg)"}, - {BUF_VIDEO_3IVX, CODEC_ID_MPEG4, "ISO MPEG-4 (3ivx, ffmpeg)"}, - {BUF_VIDEO_JPEG, CODEC_ID_MJPEG, "Motion JPEG (ffmpeg)"}, - {BUF_VIDEO_MJPEG, CODEC_ID_MJPEG, "Motion JPEG (ffmpeg)"}, - {BUF_VIDEO_MJPEG_B, CODEC_ID_MJPEGB, "Motion JPEG B (ffmpeg)"}, - {BUF_VIDEO_I263, CODEC_ID_H263I, "ITU H.263 (ffmpeg)"}, - {BUF_VIDEO_H263, CODEC_ID_H263, "H.263 (ffmpeg)"}, - {BUF_VIDEO_RV10, CODEC_ID_RV10, "Real Video 1.0 (ffmpeg)"}, - {BUF_VIDEO_RV20, CODEC_ID_RV20, "Real Video 2.0 (ffmpeg)"}, - {BUF_VIDEO_IV31, CODEC_ID_INDEO3, "Indeo Video 3.1 (ffmpeg)"}, - {BUF_VIDEO_IV32, CODEC_ID_INDEO3, "Indeo Video 3.2 (ffmpeg)"}, - {BUF_VIDEO_SORENSON_V1, CODEC_ID_SVQ1, "Sorenson Video 1 (ffmpeg)"}, - {BUF_VIDEO_SORENSON_V3, CODEC_ID_SVQ3, "Sorenson Video 3 (ffmpeg)"}, - {BUF_VIDEO_DV, CODEC_ID_DVVIDEO, "DV (ffmpeg)"}, - {BUF_VIDEO_HUFFYUV, CODEC_ID_HUFFYUV, "HuffYUV (ffmpeg)"}, - {BUF_VIDEO_VP31, CODEC_ID_VP3, "On2 VP3.1 (ffmpeg)"}, - {BUF_VIDEO_VP5, CODEC_ID_VP5, "On2 VP5 (ffmpeg)"}, - {BUF_VIDEO_VP6, CODEC_ID_VP6, "On2 VP6 (ffmpeg)"}, - {BUF_VIDEO_VP6F, CODEC_ID_VP6F, "On2 VP6 (ffmpeg)"}, - {BUF_VIDEO_4XM, CODEC_ID_4XM, "4X Video (ffmpeg)"}, - {BUF_VIDEO_CINEPAK, CODEC_ID_CINEPAK, "Cinepak (ffmpeg)"}, - {BUF_VIDEO_MSVC, CODEC_ID_MSVIDEO1, "Microsoft Video 1 (ffmpeg)"}, - {BUF_VIDEO_MSRLE, CODEC_ID_MSRLE, "Microsoft RLE (ffmpeg)"}, - {BUF_VIDEO_RPZA, CODEC_ID_RPZA, "Apple Quicktime Video/RPZA (ffmpeg)"}, - {BUF_VIDEO_CYUV, CODEC_ID_CYUV, "Creative YUV (ffmpeg)"}, - {BUF_VIDEO_ROQ, CODEC_ID_ROQ, "Id Software RoQ (ffmpeg)"}, - {BUF_VIDEO_IDCIN, CODEC_ID_IDCIN, "Id Software CIN (ffmpeg)"}, - {BUF_VIDEO_WC3, CODEC_ID_XAN_WC3, "Xan (ffmpeg)"}, - {BUF_VIDEO_VQA, CODEC_ID_WS_VQA, "Westwood Studios VQA (ffmpeg)"}, - {BUF_VIDEO_INTERPLAY, CODEC_ID_INTERPLAY_VIDEO, "Interplay MVE (ffmpeg)"}, - {BUF_VIDEO_FLI, CODEC_ID_FLIC, "FLIC Video (ffmpeg)"}, - {BUF_VIDEO_8BPS, CODEC_ID_8BPS, "Planar RGB (ffmpeg)"}, - {BUF_VIDEO_SMC, CODEC_ID_SMC, "Apple Quicktime Graphics/SMC (ffmpeg)"}, - {BUF_VIDEO_DUCKTM1, CODEC_ID_TRUEMOTION1,"Duck TrueMotion v1 (ffmpeg)"}, - {BUF_VIDEO_DUCKTM2, CODEC_ID_TRUEMOTION2,"Duck TrueMotion v2 (ffmpeg)"}, - {BUF_VIDEO_VMD, CODEC_ID_VMDVIDEO, "Sierra VMD Video (ffmpeg)"}, - {BUF_VIDEO_ZLIB, CODEC_ID_ZLIB, "ZLIB Video (ffmpeg)"}, - {BUF_VIDEO_MSZH, CODEC_ID_MSZH, "MSZH Video (ffmpeg)"}, - {BUF_VIDEO_ASV1, CODEC_ID_ASV1, "ASV v1 Video (ffmpeg)"}, - {BUF_VIDEO_ASV2, CODEC_ID_ASV2, "ASV v2 Video (ffmpeg)"}, - {BUF_VIDEO_ATIVCR1, CODEC_ID_VCR1, "ATI VCR-1 (ffmpeg)"}, - {BUF_VIDEO_FLV1, CODEC_ID_FLV1, "Flash Video (ffmpeg)"}, - {BUF_VIDEO_QTRLE, CODEC_ID_QTRLE, "Apple Quicktime Animation/RLE (ffmpeg)"}, - {BUF_VIDEO_H264, CODEC_ID_H264, "H.264/AVC (ffmpeg)"}, - {BUF_VIDEO_H261, CODEC_ID_H261, "H.261 (ffmpeg)"}, - {BUF_VIDEO_AASC, CODEC_ID_AASC, "Autodesk Video (ffmpeg)"}, - {BUF_VIDEO_LOCO, CODEC_ID_LOCO, "LOCO (ffmpeg)"}, - {BUF_VIDEO_QDRW, CODEC_ID_QDRAW, "QuickDraw (ffmpeg)"}, - {BUF_VIDEO_QPEG, CODEC_ID_QPEG, "Q-Team QPEG (ffmpeg)"}, - {BUF_VIDEO_TSCC, CODEC_ID_TSCC, "TechSmith Video (ffmpeg)"}, - {BUF_VIDEO_ULTI, CODEC_ID_ULTI, "IBM UltiMotion (ffmpeg)"}, - {BUF_VIDEO_WNV1, CODEC_ID_WNV1, "Winnow Video (ffmpeg)"}, - {BUF_VIDEO_XL, CODEC_ID_VIXL, "Miro/Pinnacle VideoXL (ffmpeg)"}, - {BUF_VIDEO_RT21, CODEC_ID_INDEO2, "Indeo/RealTime 2 (ffmpeg)"}, - {BUF_VIDEO_FPS1, CODEC_ID_FRAPS, "Fraps (ffmpeg)"}, - {BUF_VIDEO_MPEG, CODEC_ID_MPEG1VIDEO, "MPEG 1/2 (ffmpeg)"}, - {BUF_VIDEO_CSCD, CODEC_ID_CSCD, "CamStudio (ffmpeg)"}, - {BUF_VIDEO_AVS, CODEC_ID_AVS, "AVS (ffmpeg)"}, - {BUF_VIDEO_ALGMM, CODEC_ID_MMVIDEO, "American Laser Games MM (ffmpeg)"}, - {BUF_VIDEO_ZMBV, CODEC_ID_ZMBV, "Zip Motion Blocks Video (ffmpeg)"}, - {BUF_VIDEO_SMACKER, CODEC_ID_SMACKVIDEO, "Smacker (ffmpeg)"}, - {BUF_VIDEO_NUV, CODEC_ID_NUV, "NuppelVideo (ffmpeg)"}, - {BUF_VIDEO_KMVC, CODEC_ID_KMVC, "Karl Morton's Video Codec (ffmpeg)"}, - {BUF_VIDEO_FLASHSV, CODEC_ID_FLASHSV, "Flash Screen Video (ffmpeg)"}, - {BUF_VIDEO_CAVS, CODEC_ID_CAVS, "Chinese AVS (ffmpeg)"}, -}; - - -static void init_video_codec (ff_video_decoder_t *this, unsigned int codec_type) { - size_t i; - - /* find the decoder */ - this->codec = NULL; - - for(i = 0; i < sizeof(ff_video_lookup)/sizeof(ff_codec_t); i++) - if(ff_video_lookup[i].type == codec_type) { - pthread_mutex_lock(&ffmpeg_lock); - this->codec = avcodec_find_decoder(ff_video_lookup[i].id); - pthread_mutex_unlock(&ffmpeg_lock); - _x_meta_info_set_utf8(this->stream, XINE_META_INFO_VIDEOCODEC, - ff_video_lookup[i].name); - break; - } - - if (!this->codec) { - xprintf (this->stream->xine, XINE_VERBOSITY_LOG, - _("ffmpeg_video_dec: couldn't find ffmpeg decoder for buf type 0x%X\n"), - codec_type); - _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_HANDLED, 0); - return; - } - - lprintf("lavc decoder found\n"); - - /* force (width % 8 == 0), otherwise there will be - * display problems with Xv. - */ - this->bih.biWidth = (this->bih.biWidth + 1) & (~1); - - this->context->width = this->bih.biWidth; - this->context->height = this->bih.biHeight; - this->context->stream_codec_tag = this->context->codec_tag = - _x_stream_info_get(this->stream, XINE_STREAM_INFO_VIDEO_FOURCC); - - - /* Some codecs (eg rv10) copy flags in init so it's necessary to set - * this flag here in case we are going to use direct rendering */ - if(this->codec->capabilities & CODEC_CAP_DR1) { - this->context->flags |= CODEC_FLAG_EMU_EDGE; - } - - pthread_mutex_lock(&ffmpeg_lock); - if (avcodec_open (this->context, this->codec) < 0) { - pthread_mutex_unlock(&ffmpeg_lock); - xprintf (this->stream->xine, XINE_VERBOSITY_LOG, - _("ffmpeg_video_dec: couldn't open decoder\n")); - free(this->context); - this->context = NULL; - _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_HANDLED, 0); - return; - } - pthread_mutex_unlock(&ffmpeg_lock); - - lprintf("lavc decoder opened\n"); - - this->decoder_ok = 1; - - if ((codec_type != BUF_VIDEO_MPEG) && - (codec_type != BUF_VIDEO_DV)) { - - if (!this->bih.biWidth || !this->bih.biHeight) { - this->bih.biWidth = this->context->width; - this->bih.biHeight = this->context->height; - } - - - set_stream_info(this); - } - - this->stream->video_out->open (this->stream->video_out, this->stream); - - this->skipframes = 0; - - /* enable direct rendering by default */ - this->output_format = XINE_IMGFMT_YV12; -#ifdef ENABLE_DIRECT_RENDERING - if( this->codec->capabilities & CODEC_CAP_DR1 && this->codec->id != CODEC_ID_H264 ) { - this->context->get_buffer = get_buffer; - this->context->release_buffer = release_buffer; - xprintf(this->stream->xine, XINE_VERBOSITY_LOG, - _("ffmpeg_video_dec: direct rendering enabled\n")); - } -#endif - - /* flag for interlaced streams */ - this->frame_flags = 0; - /* FIXME: which codecs can be interlaced? - FIXME: check interlaced DCT and other codec specific info. */ - switch( codec_type ) { - case BUF_VIDEO_DV: - this->frame_flags |= VO_INTERLACED_FLAG; - break; - case BUF_VIDEO_MPEG: - this->frame_flags |= VO_INTERLACED_FLAG; - break; - case BUF_VIDEO_MJPEG: - this->frame_flags |= VO_INTERLACED_FLAG; - break; - case BUF_VIDEO_HUFFYUV: - this->frame_flags |= VO_INTERLACED_FLAG; - break; - } - -} - -static void pp_quality_cb(void *user_data, xine_cfg_entry_t *entry) { - ff_video_class_t *class = (ff_video_class_t *) user_data; - - class->pp_quality = entry->num_value; -} - -static void pp_change_quality (ff_video_decoder_t *this) { - this->pp_quality = this->class->pp_quality; - - if(this->pp_available && this->pp_quality) { - if(!this->pp_context && this->context) - this->pp_context = pp_get_context(this->context->width, this->context->height, - this->pp_flags); - if(this->pp_mode) - pp_free_mode(this->pp_mode); - - this->pp_mode = pp_get_mode_by_name_and_quality("hb:a,vb:a,dr:a", - this->pp_quality); - } else { - if(this->pp_mode) { - pp_free_mode(this->pp_mode); - this->pp_mode = NULL; - } - - if(this->pp_context) { - pp_free_context(this->pp_context); - this->pp_context = NULL; - } - } -} - -static void init_postprocess (ff_video_decoder_t *this) { - uint32_t cpu_caps; - - /* Allow post processing on mpeg-4 (based) codecs */ - switch(this->codec->id) { - case CODEC_ID_MPEG4: - case CODEC_ID_MSMPEG4V1: - case CODEC_ID_MSMPEG4V2: - case CODEC_ID_MSMPEG4V3: - case CODEC_ID_WMV1: - case CODEC_ID_WMV2: - this->pp_available = 1; - break; - default: - this->pp_available = 0; - break; - } - - /* Detect what cpu accel we have */ - cpu_caps = xine_mm_accel(); - this->pp_flags = PP_FORMAT_420; - - if(cpu_caps & MM_ACCEL_X86_MMX) - this->pp_flags |= PP_CPU_CAPS_MMX; - - if(cpu_caps & MM_ACCEL_X86_MMXEXT) - this->pp_flags |= PP_CPU_CAPS_MMX2; - - if(cpu_caps & MM_ACCEL_X86_3DNOW) - this->pp_flags |= PP_CPU_CAPS_3DNOW; - - /* Set level */ - pp_change_quality(this); -} - -static int ff_handle_mpeg_sequence(ff_video_decoder_t *this, mpeg_parser_t *parser) { - - /* - * init codec - */ - if (this->decoder_init_mode) { - _x_meta_info_set_utf8(this->stream, XINE_META_INFO_VIDEOCODEC, - "mpeg-1 (ffmpeg)"); - - init_video_codec (this, BUF_VIDEO_MPEG); - this->decoder_init_mode = 0; - } - - /* frame format change */ - if ((parser->width != this->bih.biWidth) || - (parser->height != this->bih.biHeight) || - (parser->frame_aspect_ratio != this->aspect_ratio)) { - xine_event_t event; - xine_format_change_data_t data; - - this->bih.biWidth = parser->width; - this->bih.biHeight = parser->height; - this->aspect_ratio = parser->frame_aspect_ratio; - this->aspect_ratio_prio = 2; - lprintf("mpeg seq aspect ratio: %f\n", this->aspect_ratio); - set_stream_info(this); - - event.type = XINE_EVENT_FRAME_FORMAT_CHANGE; - event.stream = this->stream; - event.data = &data; - event.data_length = sizeof(data); - data.width = this->bih.biWidth; - data.height = this->bih.biHeight; - data.aspect = this->aspect_ratio; - data.pan_scan = 0; - xine_event_send(this->stream, &event); - } - this->video_step = this->mpeg_parser->frame_duration; - - return 1; -} - -static void ff_convert_frame(ff_video_decoder_t *this, vo_frame_t *img) { - int y; - uint8_t *dy, *du, *dv, *sy, *su, *sv; - - dy = img->base[0]; - du = img->base[1]; - dv = img->base[2]; - sy = this->av_frame->data[0]; - su = this->av_frame->data[1]; - sv = this->av_frame->data[2]; - - if (this->context->pix_fmt == PIX_FMT_YUV410P) { - - yuv9_to_yv12( - /* Y */ - this->av_frame->data[0], - this->av_frame->linesize[0], - img->base[0], - img->pitches[0], - /* U */ - this->av_frame->data[1], - this->av_frame->linesize[1], - img->base[1], - img->pitches[1], - /* V */ - this->av_frame->data[2], - this->av_frame->linesize[2], - img->base[2], - img->pitches[2], - /* width x height */ - img->width, - img->height); - - } else if (this->context->pix_fmt == PIX_FMT_YUV411P) { - - yuv411_to_yv12( - /* Y */ - this->av_frame->data[0], - this->av_frame->linesize[0], - img->base[0], - img->pitches[0], - /* U */ - this->av_frame->data[1], - this->av_frame->linesize[1], - img->base[1], - img->pitches[1], - /* V */ - this->av_frame->data[2], - this->av_frame->linesize[2], - img->base[2], - img->pitches[2], - /* width x height */ - img->width, - img->height); - - } else if (this->context->pix_fmt == PIX_FMT_RGBA32) { - - int x, plane_ptr = 0; - uint32_t *argb_pixels; - uint32_t argb; - - for(y = 0; y < img->height; y++) { - argb_pixels = (uint32_t *)sy; - for(x = 0; x < img->width; x++) { - uint8_t r, g, b; - - /* this is endian-safe as the ARGB pixels are stored in - * machine order */ - argb = *argb_pixels++; - r = (argb >> 16) & 0xFF; - g = (argb >> 8) & 0xFF; - b = (argb >> 0) & 0xFF; - - this->yuv.y[plane_ptr] = COMPUTE_Y(r, g, b); - this->yuv.u[plane_ptr] = COMPUTE_U(r, g, b); - this->yuv.v[plane_ptr] = COMPUTE_V(r, g, b); - plane_ptr++; - } - sy += this->av_frame->linesize[0]; - } - - yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); - - } else if (this->context->pix_fmt == PIX_FMT_RGB565) { - - int x, plane_ptr = 0; - uint8_t *src; - uint16_t pixel16; - - for(y = 0; y < img->height; y++) { - src = sy; - for(x = 0; x < img->width; x++) { - uint8_t r, g, b; - - /* a 16-bit RGB565 pixel is supposed to be stored in native-endian - * byte order; the following should be endian-safe */ - pixel16 = *((uint16_t *)src); - src += 2; - b = (pixel16 << 3) & 0xFF; - g = (pixel16 >> 3) & 0xFF; - r = (pixel16 >> 8) & 0xFF; - - this->yuv.y[plane_ptr] = COMPUTE_Y(r, g, b); - this->yuv.u[plane_ptr] = COMPUTE_U(r, g, b); - this->yuv.v[plane_ptr] = COMPUTE_V(r, g, b); - plane_ptr++; - } - sy += this->av_frame->linesize[0]; - } - - yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); - - } else if (this->context->pix_fmt == PIX_FMT_RGB555) { - - int x, plane_ptr = 0; - uint8_t *src; - uint16_t pixel16; - - for(y = 0; y < img->height; y++) { - src = sy; - for(x = 0; x < img->width; x++) { - uint8_t r, g, b; - - /* a 16-bit RGB555 pixel is supposed to be stored in native-endian - * byte order; the following should be endian-safe */ - pixel16 = *((uint16_t *)src); - src += 2; - b = (pixel16 << 3) & 0xFF; - g = (pixel16 >> 2) & 0xFF; - r = (pixel16 >> 7) & 0xFF; - - this->yuv.y[plane_ptr] = COMPUTE_Y(r, g, b); - this->yuv.u[plane_ptr] = COMPUTE_U(r, g, b); - this->yuv.v[plane_ptr] = COMPUTE_V(r, g, b); - plane_ptr++; - } - sy += this->av_frame->linesize[0]; - } - - yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); - - } else if (this->context->pix_fmt == PIX_FMT_BGR24) { - - int x, plane_ptr = 0; - uint8_t *src; - - for(y = 0; y < img->height; y++) { - src = sy; - for(x = 0; x < img->width; x++) { - uint8_t r, g, b; - - b = *src++; - g = *src++; - r = *src++; - - this->yuv.y[plane_ptr] = COMPUTE_Y(r, g, b); - this->yuv.u[plane_ptr] = COMPUTE_U(r, g, b); - this->yuv.v[plane_ptr] = COMPUTE_V(r, g, b); - plane_ptr++; - } - sy += this->av_frame->linesize[0]; - } - - yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); - - } else if (this->context->pix_fmt == PIX_FMT_RGB24) { - - int x, plane_ptr = 0; - uint8_t *src; - - for(y = 0; y < img->height; y++) { - src = sy; - for(x = 0; x < img->width; x++) { - uint8_t r, g, b; - - r = *src++; - g = *src++; - b = *src++; - - this->yuv.y[plane_ptr] = COMPUTE_Y(r, g, b); - this->yuv.u[plane_ptr] = COMPUTE_U(r, g, b); - this->yuv.v[plane_ptr] = COMPUTE_V(r, g, b); - plane_ptr++; - } - sy += this->av_frame->linesize[0]; - } - - yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); - - } else if (this->context->pix_fmt == PIX_FMT_PAL8) { - - int x, plane_ptr = 0; - uint8_t *src; - uint8_t pixel; - uint32_t *palette32 = (uint32_t *)su; /* palette is in data[1] */ - uint32_t rgb_color; - uint8_t r, g, b; - uint8_t y_palette[256]; - uint8_t u_palette[256]; - uint8_t v_palette[256]; - - for (x = 0; x < 256; x++) { - rgb_color = palette32[x]; - b = rgb_color & 0xFF; - rgb_color >>= 8; - g = rgb_color & 0xFF; - rgb_color >>= 8; - r = rgb_color & 0xFF; - y_palette[x] = COMPUTE_Y(r, g, b); - u_palette[x] = COMPUTE_U(r, g, b); - v_palette[x] = COMPUTE_V(r, g, b); - } - - for(y = 0; y < img->height; y++) { - src = sy; - for(x = 0; x < img->width; x++) { - pixel = *src++; - - this->yuv.y[plane_ptr] = y_palette[pixel]; - this->yuv.u[plane_ptr] = u_palette[pixel]; - this->yuv.v[plane_ptr] = v_palette[pixel]; - plane_ptr++; - } - sy += this->av_frame->linesize[0]; - } - - yuv444_to_yuy2(&this->yuv, img->base[0], img->pitches[0]); - - } else { - - for (y=0; yheight; y++) { - xine_fast_memcpy (dy, sy, img->width); - - dy += img->pitches[0]; - - sy += this->av_frame->linesize[0]; - } - - for (y=0; y<(img->height/2); y++) { - - if (this->context->pix_fmt != PIX_FMT_YUV444P) { - - xine_fast_memcpy (du, su, img->width/2); - xine_fast_memcpy (dv, sv, img->width/2); - - } else { - - int x; - uint8_t *src; - uint8_t *dst; - - /* subsample */ - - src = su; dst = du; - for (x=0; x<(img->width/2); x++) { - *dst = *src; - dst++; - src += 2; - } - src = sv; dst = dv; - for (x=0; x<(img->width/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_frame->linesize[1]; - sv += 2*this->av_frame->linesize[2]; - } else { - su += this->av_frame->linesize[1]; - sv += this->av_frame->linesize[2]; - } - } - } -} - -static void ff_check_bufsize (ff_video_decoder_t *this, int size) { - if (size > this->bufsize) { - this->bufsize = size + size / 2; - xprintf(this->stream->xine, XINE_VERBOSITY_LOG, - _("ffmpeg_video_dec: increasing buffer to %d to avoid overflow.\n"), - this->bufsize); - this->buf = realloc(this->buf, this->bufsize + FF_INPUT_BUFFER_PADDING_SIZE ); - } -} - -static void ff_handle_preview_buffer (ff_video_decoder_t *this, buf_element_t *buf) { - int codec_type; - - lprintf ("preview buffer\n"); - - codec_type = buf->type & 0xFFFF0000; - if (codec_type == BUF_VIDEO_MPEG) { - this->is_mpeg12 = 1; - if ( this->mpeg_parser == NULL ) { - this->mpeg_parser = xine_xmalloc(sizeof(mpeg_parser_t)); - mpeg_parser_init(this->mpeg_parser); - this->decoder_init_mode = 0; - } - } - - if (this->decoder_init_mode && !this->is_mpeg12) { - init_video_codec(this, codec_type); - init_postprocess(this); - this->decoder_init_mode = 0; - } -} - -static void ff_handle_header_buffer (ff_video_decoder_t *this, buf_element_t *buf) { - - lprintf ("header buffer\n"); - - /* accumulate data */ - ff_check_bufsize(this, this->size + buf->size); - xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size); - this->size += buf->size; - - if (buf->decoder_flags & BUF_FLAG_FRAME_END) { - int codec_type; - - lprintf ("header complete\n"); - codec_type = buf->type & 0xFFFF0000; - - if (buf->decoder_flags & BUF_FLAG_STDHEADER) { - - lprintf("standard header\n"); - - /* init package containing bih */ - memcpy ( &this->bih, this->buf, sizeof(xine_bmiheader) ); - - if (this->bih.biSize > sizeof(xine_bmiheader)) { - this->context->extradata_size = this->bih.biSize - sizeof(xine_bmiheader); - this->context->extradata = malloc(this->context->extradata_size + - FF_INPUT_BUFFER_PADDING_SIZE); - memcpy(this->context->extradata, this->buf + sizeof(xine_bmiheader), - this->context->extradata_size); - } - - this->context->bits_per_sample = this->bih.biBitCount; - - } else { - - switch (codec_type) { - case BUF_VIDEO_RV10: - case BUF_VIDEO_RV20: - this->bih.biWidth = BE_16(&this->buf[12]); - this->bih.biHeight = BE_16(&this->buf[14]); - - this->context->sub_id = BE_32(&this->buf[30]); - - this->context->slice_offset = xine_xmalloc(sizeof(int)*SLICE_OFFSET_SIZE); - this->slice_offset_size = SLICE_OFFSET_SIZE; - - lprintf("w=%d, h=%d\n", this->bih.biWidth, this->bih.biHeight); - - break; - default: - xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, - "ffmpeg_video_dec: unknown header for buf type 0x%X\n", codec_type); - return; - } - } - - /* reset accumulator */ - this->size = 0; - } -} - -static void ff_handle_special_buffer (ff_video_decoder_t *this, buf_element_t *buf) { - /* take care of all the various types of special buffers - * note that order is important here */ - lprintf("special buffer\n"); - - if (buf->decoder_info[1] == BUF_SPECIAL_STSD_ATOM && - !this->context->extradata_size) { - - lprintf("BUF_SPECIAL_STSD_ATOM\n"); - this->context->extradata_size = buf->decoder_info[2]; - this->context->extradata = xine_xmalloc(buf->decoder_info[2] + - FF_INPUT_BUFFER_PADDING_SIZE); - memcpy(this->context->extradata, buf->decoder_info_ptr[2], - buf->decoder_info[2]); - - } else if (buf->decoder_info[1] == BUF_SPECIAL_DECODER_CONFIG && - !this->context->extradata_size) { - - lprintf("BUF_SPECIAL_DECODER_CONFIG\n"); - this->context->extradata_size = buf->decoder_info[2]; - this->context->extradata = xine_xmalloc(buf->decoder_info[2] + - FF_INPUT_BUFFER_PADDING_SIZE); - memcpy(this->context->extradata, buf->decoder_info_ptr[2], - buf->decoder_info[2]); - - } else if (buf->decoder_info[1] == BUF_SPECIAL_PALETTE) { - unsigned int i; - - palette_entry_t *demuxer_palette; - AVPaletteControl *decoder_palette; - - lprintf("BUF_SPECIAL_PALETTE\n"); - this->context->palctrl = &this->palette_control; - decoder_palette = (AVPaletteControl *)this->context->palctrl; - demuxer_palette = (palette_entry_t *)buf->decoder_info_ptr[2]; - - for (i = 0; i < buf->decoder_info[2]; i++) { - decoder_palette->palette[i] = - (demuxer_palette[i].r << 16) | - (demuxer_palette[i].g << 8) | - (demuxer_palette[i].b << 0); - } - decoder_palette->palette_changed = 1; - - } else if (buf->decoder_info[1] == BUF_SPECIAL_RV_CHUNK_TABLE) { - int i; - - lprintf("BUF_SPECIAL_RV_CHUNK_TABLE\n"); - this->context->slice_count = buf->decoder_info[2]+1; - - lprintf("slice_count=%d\n", this->context->slice_count); - - if(this->context->slice_count > this->slice_offset_size) { - this->context->slice_offset = realloc(this->context->slice_offset, - sizeof(int)*this->context->slice_count); - this->slice_offset_size = this->context->slice_count; - } - - for(i = 0; i < this->context->slice_count; i++) { - this->context->slice_offset[i] = - ((uint32_t *) buf->decoder_info_ptr[2])[(2*i)+1]; - lprintf("slice_offset[%d]=%d\n", i, this->context->slice_offset[i]); - } - } -} - -static void ff_handle_mpeg12_buffer (ff_video_decoder_t *this, buf_element_t *buf) { - - vo_frame_t *img; - int free_img; - int got_picture, len; - int offset = 0; - int flush = 0; - int size = buf->size; - - lprintf("handle_mpeg12_buffer\n"); - - while ((size > 0) || (flush == 1)) { - - uint8_t *current; - int next_flush; - - got_picture = 0; - if (!flush) { - current = mpeg_parser_decode_data(this->mpeg_parser, - buf->content + offset, buf->content + offset + size, - &next_flush); - } else { - current = buf->content + offset + size; /* end of the buffer */ - next_flush = 0; - } - if (current == NULL) { - lprintf("current == NULL\n"); - return; - } - - if (this->mpeg_parser->has_sequence) { - ff_handle_mpeg_sequence(this, this->mpeg_parser); - } - - if (!this->decoder_ok) - return; - - if (flush) { - lprintf("flush lavc buffers\n"); - /* hack: ffmpeg outputs the last frame if size=0 */ - this->mpeg_parser->buffer_size = 0; - } - - /* skip decoding b frames if too late */ - this->context->hurry_up = (this->skipframes > 0); - - lprintf("avcodec_decode_video: size=%d\n", this->mpeg_parser->buffer_size); - len = avcodec_decode_video (this->context, this->av_frame, - &got_picture, this->mpeg_parser->chunk_buffer, - this->mpeg_parser->buffer_size); - lprintf("avcodec_decode_video: decoded_size=%d, got_picture=%d\n", - len, got_picture); - len = current - buf->content - offset; - lprintf("avcodec_decode_video: consumed_size=%d\n", len); - - flush = next_flush; - - if ((len < 0) || (len > buf->size)) { - xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, - "ffmpeg_video_dec: error decompressing frame\n"); - size = 0; /* draw a bad frame and exit */ - } else { - size -= len; - offset += len; - } - - if (got_picture && this->av_frame->data[0]) { - /* got a picture, draw it */ - if(!this->av_frame->opaque) { - /* indirect rendering */ - img = this->stream->video_out->get_frame (this->stream->video_out, - this->bih.biWidth, - this->bih.biHeight, - this->aspect_ratio, - this->output_format, - VO_BOTH_FIELDS|this->frame_flags); - free_img = 1; - } else { - /* DR1 */ - img = (vo_frame_t*) this->av_frame->opaque; - free_img = 0; - } - - img->pts = this->pts; - this->pts = 0; - - if (this->av_frame->repeat_pict) - img->duration = this->video_step * 3 / 2; - else - img->duration = this->video_step; - - img->crop_right = this->crop_right; - img->crop_bottom = this->crop_bottom; - - this->skipframes = img->draw(img, this->stream); - - if(free_img) - img->free(img); - - } else { - - if (this->context->hurry_up) { - /* skipped frame, output a bad frame */ - img = this->stream->video_out->get_frame (this->stream->video_out, - this->bih.biWidth, - this->bih.biHeight, - this->aspect_ratio, - this->output_format, - VO_BOTH_FIELDS|this->frame_flags); - img->pts = 0; - img->duration = this->video_step; - img->bad_frame = 1; - this->skipframes = img->draw(img, this->stream); - img->free(img); - } - } - } -} - -static void ff_handle_buffer (ff_video_decoder_t *this, buf_element_t *buf) { - uint8_t *chunk_buf = this->buf; - AVRational avr00 = {0, 1}; - - lprintf("handle_buffer\n"); - - if (!this->decoder_ok) { - if (this->decoder_init_mode) { - int codec_type = buf->type & 0xFFFF0000; - - /* init ffmpeg decoder */ - init_video_codec(this, codec_type); - init_postprocess(this); - this->decoder_init_mode = 0; - } else { - return; - } - } - - if (buf->decoder_flags & BUF_FLAG_FRAME_START) { - lprintf("BUF_FLAG_FRAME_START\n"); - this->size = 0; - } - - /* data accumulation */ - if (buf->size > 0) { - if ((this->size == 0) && - ((buf->size + FF_INPUT_BUFFER_PADDING_SIZE) < buf->max_size) && - (buf->decoder_flags & BUF_FLAG_FRAME_END)) { - /* buf contains a complete frame */ - /* no memcpy needed */ - chunk_buf = buf->content; - this->size = buf->size; - lprintf("no memcpy needed to accumulate data\n"); - } else { - /* copy data into our internal buffer */ - ff_check_bufsize(this, this->size + buf->size); - chunk_buf = this->buf; /* ff_check_bufsize might realloc this->buf */ - - xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size); - - this->size += buf->size; - lprintf("accumulate data into this->buf\n"); - } - } - - if (buf->decoder_flags & BUF_FLAG_FRAME_END) { - - vo_frame_t *img; - int free_img; - int got_picture, len; - int got_one_picture = 0; - int offset = 0; - int codec_type = buf->type & 0xFFFF0000; - - /* pad input data */ - /* note: bitstream, alt bitstream reader or something will cause - * severe mpeg4 artifacts if padding is less than 32 bits. - */ - memset(&chunk_buf[this->size], 0, FF_INPUT_BUFFER_PADDING_SIZE); - - while (this->size > 0) { - - /* DV frames can be completely skipped */ - if( codec_type == BUF_VIDEO_DV && this->skipframes ) { - this->size = 0; - got_picture = 0; - } else { - /* skip decoding b frames if too late */ - this->context->hurry_up = (this->skipframes > 0); - - lprintf("buffer size: %d\n", this->size); - len = avcodec_decode_video (this->context, this->av_frame, - &got_picture, &chunk_buf[offset], - this->size); - lprintf("consumed size: %d, got_picture: %d\n", len, got_picture); - if ((len <= 0) || (len > this->size)) { - xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, - "ffmpeg_video_dec: error decompressing frame\n"); - this->size = 0; - - } else { - - offset += len; - this->size -= len; - - if (this->size > 0) { - ff_check_bufsize(this, this->size); - memmove (this->buf, &chunk_buf[offset], this->size); - chunk_buf = this->buf; - } - } - } - - /* aspect ratio provided by ffmpeg, override previous setting */ - if ((this->aspect_ratio_prio < 2) && - av_cmp_q(this->context->sample_aspect_ratio, avr00)) { - - this->aspect_ratio = av_q2d(this->context->sample_aspect_ratio) * - (double)this->bih.biWidth / (double)this->bih.biHeight; - this->aspect_ratio_prio = 2; - lprintf("ffmpeg aspect ratio: %f\n", this->aspect_ratio); - set_stream_info(this); - } - - if (got_picture && this->av_frame->data[0]) { - /* got a picture, draw it */ - got_one_picture = 1; - if(!this->av_frame->opaque) { - /* indirect rendering */ - - /* initialize the colorspace converter */ - if (!this->cs_convert_init) { - if ((this->context->pix_fmt == PIX_FMT_RGBA32) || - (this->context->pix_fmt == PIX_FMT_RGB565) || - (this->context->pix_fmt == PIX_FMT_RGB555) || - (this->context->pix_fmt == PIX_FMT_BGR24) || - (this->context->pix_fmt == PIX_FMT_RGB24) || - (this->context->pix_fmt == PIX_FMT_PAL8)) { - this->output_format = XINE_IMGFMT_YUY2; - init_yuv_planes(&this->yuv, this->bih.biWidth, this->bih.biHeight); - this->yuv_init = 1; - } - this->cs_convert_init = 1; - } - - if (this->aspect_ratio_prio == 0) { - this->aspect_ratio = (double)this->bih.biWidth / (double)this->bih.biHeight; - this->aspect_ratio_prio = 1; - lprintf("default aspect ratio: %f\n", this->aspect_ratio); - set_stream_info(this); - } - - img = this->stream->video_out->get_frame (this->stream->video_out, - this->bih.biWidth, - this->bih.biHeight, - this->aspect_ratio, - this->output_format, - VO_BOTH_FIELDS|this->frame_flags); - free_img = 1; - } else { - /* DR1 */ - img = (vo_frame_t*) this->av_frame->opaque; - free_img = 0; - } - - /* post processing */ - if(this->pp_quality != this->class->pp_quality) - pp_change_quality(this); - - if(this->pp_available && this->pp_quality) { - - if(this->av_frame->opaque) { - /* DR1 */ - img = this->stream->video_out->get_frame (this->stream->video_out, - img->width, - img->height, - this->aspect_ratio, - this->output_format, - VO_BOTH_FIELDS|this->frame_flags); - free_img = 1; - } - - pp_postprocess(this->av_frame->data, this->av_frame->linesize, - img->base, img->pitches, - img->width, img->height, - this->av_frame->qscale_table, this->av_frame->qstride, - this->pp_mode, this->pp_context, - this->av_frame->pict_type); - - } else if (!this->av_frame->opaque) { - /* colorspace conversion or copy */ - ff_convert_frame(this, img); - } - - img->pts = this->pts; - this->pts = 0; - - /* workaround for weird 120fps streams */ - if( this->video_step == 750 ) { - /* fallback to the VIDEO_PTS_MODE */ - this->video_step = 0; - } - - if (this->av_frame->repeat_pict) - img->duration = this->video_step * 3 / 2; - else - img->duration = this->video_step; - - img->crop_right = this->crop_right; - img->crop_bottom = this->crop_bottom; - - this->skipframes = img->draw(img, this->stream); - - if(free_img) - img->free(img); - } - } - - if (!got_one_picture) { - /* skipped frame, output a bad frame */ - img = this->stream->video_out->get_frame (this->stream->video_out, - this->bih.biWidth, - this->bih.biHeight, - this->aspect_ratio, - this->output_format, - VO_BOTH_FIELDS|this->frame_flags); - img->pts = 0; - img->duration = this->video_step; - img->bad_frame = 1; - this->skipframes = img->draw(img, this->stream); - img->free(img); - } - } -} - -static void ff_decode_data (video_decoder_t *this_gen, buf_element_t *buf) { - ff_video_decoder_t *this = (ff_video_decoder_t *) this_gen; - - lprintf ("processing packet type = %08x, len = %d, decoder_flags=%08x\n", - buf->type, buf->size, buf->decoder_flags); - - if (buf->decoder_flags & BUF_FLAG_FRAMERATE) { - this->video_step = buf->decoder_info[0]; - _x_stream_info_set(this->stream, XINE_STREAM_INFO_FRAME_DURATION, this->video_step); - } - - if (buf->decoder_flags & BUF_FLAG_PREVIEW) { - - ff_handle_preview_buffer(this, buf); - - } else { - - if (buf->decoder_flags & BUF_FLAG_SPECIAL) { - - ff_handle_special_buffer(this, buf); - - } - - if (buf->decoder_flags & BUF_FLAG_HEADER) { - - ff_handle_header_buffer(this, buf); - - if (buf->decoder_flags & BUF_FLAG_ASPECT) { - if (this->aspect_ratio_prio < 3) { - this->aspect_ratio = (double)buf->decoder_info[1] / (double)buf->decoder_info[2]; - this->aspect_ratio_prio = 3; - lprintf("aspect ratio: %f\n", this->aspect_ratio); - set_stream_info(this); - } - } - - } else { - - /* decode */ - if (buf->pts) - this->pts = buf->pts; - - if (this->is_mpeg12) { - ff_handle_mpeg12_buffer(this, buf); - } else { - ff_handle_buffer(this, buf); - } - - } - } -} - -static void ff_flush (video_decoder_t *this_gen) { - lprintf ("ff_flush\n"); -} - -static void ff_reset (video_decoder_t *this_gen) { - ff_video_decoder_t *this = (ff_video_decoder_t *) this_gen; - - lprintf ("ff_reset\n"); - - this->size = 0; - - if(this->context && this->decoder_ok) - avcodec_flush_buffers(this->context); - - if (this->is_mpeg12) - mpeg_parser_reset(this->mpeg_parser); -} - -static void ff_discontinuity (video_decoder_t *this_gen) { - ff_video_decoder_t *this = (ff_video_decoder_t *) this_gen; - - lprintf ("ff_discontinuity\n"); - this->pts = 0; -} - -static void ff_dispose (video_decoder_t *this_gen) { - ff_video_decoder_t *this = (ff_video_decoder_t *) this_gen; - - lprintf ("ff_dispose\n"); - - if (this->decoder_ok) { - xine_list_iterator_t it; - AVFrame *av_frame; - - pthread_mutex_lock(&ffmpeg_lock); - avcodec_close (this->context); - pthread_mutex_unlock(&ffmpeg_lock); - - /* frame garbage collector here - workaround for buggy ffmpeg codecs that - * don't release their DR1 frames */ - while( (it = xine_list_front(this->dr1_frames)) != NULL ) - { - av_frame = (AVFrame *)xine_list_get_value(this->dr1_frames, it); - release_buffer(this->context, av_frame); - } - - this->stream->video_out->close(this->stream->video_out, this->stream); - this->decoder_ok = 0; - } - - if(this->context && this->context->slice_offset) - free(this->context->slice_offset); - - if(this->context && this->context->extradata) - free(this->context->extradata); - - if(this->yuv_init) - free_yuv_planes(&this->yuv); - - if( this->context ) - free( this->context ); - - if( this->av_frame ) - free( this->av_frame ); - - if (this->buf) - free(this->buf); - this->buf = NULL; - - if(this->pp_context) - pp_free_context(this->pp_context); - - if(this->pp_mode) - pp_free_mode(this->pp_mode); - - mpeg_parser_dispose(this->mpeg_parser); - - xine_list_delete(this->dr1_frames); - - free (this_gen); -} - -static video_decoder_t *ff_video_open_plugin (video_decoder_class_t *class_gen, xine_stream_t *stream) { - - ff_video_decoder_t *this ; - - lprintf ("open_plugin\n"); - - this = (ff_video_decoder_t *) xine_xmalloc (sizeof (ff_video_decoder_t)); - - this->video_decoder.decode_data = ff_decode_data; - this->video_decoder.flush = ff_flush; - this->video_decoder.reset = ff_reset; - this->video_decoder.discontinuity = ff_discontinuity; - this->video_decoder.dispose = ff_dispose; - this->size = 0; - - this->stream = stream; - this->class = (ff_video_class_t *) class_gen; - - this->av_frame = avcodec_alloc_frame(); - this->context = avcodec_alloc_context(); - this->context->opaque = this; - this->context->palctrl = NULL; - - this->decoder_ok = 0; - this->decoder_init_mode = 1; - this->buf = xine_xmalloc(VIDEOBUFSIZE + FF_INPUT_BUFFER_PADDING_SIZE); - this->bufsize = VIDEOBUFSIZE; - - this->is_mpeg12 = 0; - this->aspect_ratio = 0; - - this->pp_quality = 0; - this->pp_context = NULL; - this->pp_mode = NULL; - - this->mpeg_parser = NULL; - - this->dr1_frames = xine_list_new(); - - return &this->video_decoder; -} - -static char *ff_video_get_identifier (video_decoder_class_t *this) { - return "ffmpeg video"; -} - -static char *ff_video_get_description (video_decoder_class_t *this) { - return "ffmpeg based video decoder plugin"; -} - -static void ff_video_dispose_class (video_decoder_class_t *this) { - free (this); -} - -void *init_video_plugin (xine_t *xine, void *data) { - - ff_video_class_t *this; - config_values_t *config; - - this = (ff_video_class_t *) xine_xmalloc (sizeof (ff_video_class_t)); - - this->decoder_class.open_plugin = ff_video_open_plugin; - this->decoder_class.get_identifier = ff_video_get_identifier; - this->decoder_class.get_description = ff_video_get_description; - this->decoder_class.dispose = ff_video_dispose_class; - this->xine = xine; - - pthread_once( &once_control, init_once_routine ); - - /* Configuration for post processing quality - default to mid (3) for the - * moment */ - config = xine->config; - - this->pp_quality = xine->config->register_range(config, "video.processing.ffmpeg_pp_quality", 3, - 0, PP_QUALITY_MAX, - _("MPEG-4 postprocessing quality"), - _("You can adjust the amount of post processing applied to MPEG-4 video.\n" - "Higher values result in better quality, but need more CPU. Lower values may " - "result in image defects like block artifacts. For high quality content, " - "too heavy post processing can actually make the image worse by blurring it " - "too much."), - 10, pp_quality_cb, this); - - return this; -} - -static uint32_t supported_video_types[] = { - #ifdef CONFIG_MSMPEG4V1_DECODER - BUF_VIDEO_MSMPEG4_V1, - #endif - #ifdef CONFIG_MSMPEG4V2_DECODER - BUF_VIDEO_MSMPEG4_V2, - #endif - #ifdef CONFIG_MSMPEG4V3_DECODER - BUF_VIDEO_MSMPEG4_V3, - #endif - #ifdef CONFIG_WMV1_DECODER - BUF_VIDEO_WMV7, - #endif - #ifdef CONFIG_WMV2_DECODER - BUF_VIDEO_WMV8, - #endif - #ifdef CONFIG_WMV3_DECODER - BUF_VIDEO_WMV9, - #endif - #ifdef CONFIG_MPEG4_DECODER - BUF_VIDEO_MPEG4, - #endif - #ifdef CONFIG_MPEG4_DECODER - BUF_VIDEO_XVID, - #endif - #ifdef CONFIG_MPEG4_DECODER - BUF_VIDEO_DIVX5, - #endif - #ifdef CONFIG_MPEG4_DECODER - BUF_VIDEO_3IVX, - #endif - #ifdef CONFIG_MJPEG_DECODER - BUF_VIDEO_JPEG, - #endif - #ifdef CONFIG_MJPEG_DECODER - BUF_VIDEO_MJPEG, - #endif - #ifdef CONFIG_MJPEGB_DECODER - BUF_VIDEO_MJPEG_B, - #endif - #ifdef CONFIG_H263I_DECODER - BUF_VIDEO_I263, - #endif - #ifdef CONFIG_H263_DECODER - BUF_VIDEO_H263, - #endif - #ifdef CONFIG_RV10_DECODER - BUF_VIDEO_RV10, - #endif - #ifdef CONFIG_RV20_DECODER - BUF_VIDEO_RV20, - #endif - #ifdef CONFIG_INDEO3_DECODER - BUF_VIDEO_IV31, - #endif - #ifdef CONFIG_INDEO3_DECODER - BUF_VIDEO_IV32, - #endif - #ifdef CONFIG_SVQ1_DECODER - BUF_VIDEO_SORENSON_V1, - #endif - #ifdef CONFIG_SVQ3_DECODER - BUF_VIDEO_SORENSON_V3, - #endif - #ifdef CONFIG_DVVIDEO_DECODER - BUF_VIDEO_DV, - #endif - #ifdef CONFIG_HUFFYUV_DECODER - BUF_VIDEO_HUFFYUV, - #endif - #ifdef CONFIG_VP3_DECODER - BUF_VIDEO_VP31, - #endif - #ifdef CONFIG_VP5_DECODER - BUF_VIDEO_VP5, - #endif - #ifdef CONFIG_VP6_DECODER - BUF_VIDEO_VP6, - BUF_VIDEO_VP6F, - #endif - #ifdef CONFIG_4XM_DECODER - BUF_VIDEO_4XM, - #endif - #ifdef CONFIG_CINEPAK_DECODER - BUF_VIDEO_CINEPAK, - #endif - #ifdef CONFIG_MSVIDEO1_DECODER - BUF_VIDEO_MSVC, - #endif - #ifdef CONFIG_MSRLE_DECODER - BUF_VIDEO_MSRLE, - #endif - #ifdef CONFIG_RPZA_DECODER - BUF_VIDEO_RPZA, - #endif - #ifdef CONFIG_CYUV_DECODER - BUF_VIDEO_CYUV, - #endif - #ifdef CONFIG_ROQ_DECODER - BUF_VIDEO_ROQ, - #endif - #ifdef CONFIG_IDCIN_DECODER - BUF_VIDEO_IDCIN, - #endif - #ifdef CONFIG_XAN_WC3_DECODER - BUF_VIDEO_WC3, - #endif - #ifdef CONFIG_WS_VQA_DECODER - BUF_VIDEO_VQA, - #endif - #ifdef CONFIG_INTERPLAY_VIDEO_DECODER - BUF_VIDEO_INTERPLAY, - #endif - #ifdef CONFIG_FLIC_DECODER - BUF_VIDEO_FLI, - #endif - #ifdef CONFIG_8BPS_DECODER - BUF_VIDEO_8BPS, - #endif - #ifdef CONFIG_SMC_DECODER - BUF_VIDEO_SMC, - #endif - #ifdef CONFIG_TRUEMOTION1_DECODER - BUF_VIDEO_DUCKTM1, - #endif - #ifdef CONFIG_TRUEMOTION2_DECODER - BUF_VIDEO_DUCKTM2, - #endif - #ifdef CONFIG_VMDVIDEO_DECODER - BUF_VIDEO_VMD, - #endif - #ifdef CONFIG_ZLIB_DECODER - BUF_VIDEO_ZLIB, - #endif - #ifdef CONFIG_MSZH_DECODER - BUF_VIDEO_MSZH, - #endif - #ifdef CONFIG_ASV1_DECODER - BUF_VIDEO_ASV1, - #endif - #ifdef CONFIG_ASV2_DECODER - BUF_VIDEO_ASV2, - #endif - #ifdef CONFIG_VCR1_DECODER - BUF_VIDEO_ATIVCR1, - #endif - #ifdef CONFIG_FLV_DECODER - BUF_VIDEO_FLV1, - #endif - #ifdef CONFIG_QTRLE_DECODER - BUF_VIDEO_QTRLE, - #endif - #ifdef CONFIG_H264_DECODER - BUF_VIDEO_H264, - #endif - #ifdef CONFIG_H261_DECODER - BUF_VIDEO_H261, - #endif - #ifdef CONFIG_AASC_DECODER - BUF_VIDEO_AASC, - #endif - #ifdef CONFIG_LOCO_DECODER - BUF_VIDEO_LOCO, - #endif - #ifdef CONFIG_QDRAW_DECODER - BUF_VIDEO_QDRW, - #endif - #ifdef CONFIG_QPEG_DECODER - BUF_VIDEO_QPEG, - #endif - #ifdef CONFIG_TSCC_DECODER - BUF_VIDEO_TSCC, - #endif - #ifdef CONFIG_ULTI_DECODER - BUF_VIDEO_ULTI, - #endif - #ifdef CONFIG_WNV1_DECODER - BUF_VIDEO_WNV1, - #endif - #ifdef CONFIG_VIXL_DECODER - BUF_VIDEO_XL, - #endif - #ifdef CONFIG_INDEO2_DECODER - BUF_VIDEO_RT21, - #endif - #ifdef CONFIG_FRAPS_DECODER - BUF_VIDEO_FPS1, - #endif - #ifdef CONFIG_MPEG1VIDEO_DECODER - BUF_VIDEO_MPEG, - #endif - #ifdef CONFIG_CSCD_DECODER - BUF_VIDEO_CSCD, - #endif - #ifdef CONFIG_AVS_DECODER - BUF_VIDEO_AVS, - #endif - #ifdef CONFIG_MMVIDEO_DECODER - BUF_VIDEO_ALGMM, - #endif - #ifdef CONFIG_ZMBV_DECODER - BUF_VIDEO_ZMBV, - #endif - #ifdef CONFIG_SMACKVIDEO_DECODER - BUF_VIDEO_SMACKER, - #endif - #ifdef CONFIG_NUV_DECODER - BUF_VIDEO_NUV, - #endif - #ifdef CONFIG_KMVC_DECODER - BUF_VIDEO_KMVC, - #endif - #ifdef CONFIG_FLASHSV_DECODER - BUF_VIDEO_FLASHSV, - #endif - #ifdef CONFIG_CAVS_DECODER - BUF_VIDEO_CAVS, - #endif - - 0 -}; - -static uint32_t wmv8_video_types[] = { - BUF_VIDEO_WMV8, - 0 -}; - -static uint32_t wmv9_video_types[] = { - BUF_VIDEO_WMV9, - 0 -}; - -decoder_info_t dec_info_ffmpeg_video = { - supported_video_types, /* supported types */ - 6 /* priority */ -}; - -decoder_info_t dec_info_ffmpeg_wmv8 = { - wmv8_video_types, /* supported types */ - 0 /* priority */ -}; - -decoder_info_t dec_info_ffmpeg_wmv9 = { - wmv9_video_types, /* supported types */ - 0 /* priority */ -}; diff --git a/src/libffmpeg/xine_decoder.c b/src/libffmpeg/xine_decoder.c deleted file mode 100644 index 2eeb9746b..000000000 --- a/src/libffmpeg/xine_decoder.c +++ /dev/null @@ -1,329 +0,0 @@ -/* - * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - * - * $Id: xine_decoder.c,v 1.173 2007/01/13 21:19:52 miguelfreitas Exp $ - * - * xine decoder plugin using ffmpeg - * - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#include "ffmpeg_config.h" -#endif - -#include "xine_internal.h" - -#include "xine_decoder.h" - -/* - * common initialisation - */ - -pthread_once_t once_control = PTHREAD_ONCE_INIT; -pthread_mutex_t ffmpeg_lock; - -#ifndef HAVE_FFMPEG - -#define REGISTER_ENCODER(X,x) \ - if(ENABLE_##X##_ENCODER) register_avcodec(&x##_encoder) -#define REGISTER_DECODER(X,x) \ - if(ENABLE_##X##_DECODER) register_avcodec(&x##_decoder) -#define REGISTER_ENCDEC(X,x) REGISTER_ENCODER(X,x); REGISTER_DECODER(X,x) - -#define REGISTER_PARSER(X,x) \ - if(ENABLE_##X##_PARSER) av_register_codec_parser(&x##_parser) - -/* If you do not call this function, then you can select exactly which - formats you want to support */ - -/** - * simple call to register all the codecs. - */ -void avcodec_register_all(void) -{ - static int inited = 0; - - if (inited != 0) - return; - inited = 1; - - /* video codecs */ - REGISTER_DECODER(AASC, aasc); - REGISTER_ENCDEC (ASV1, asv1); - REGISTER_ENCDEC (ASV2, asv2); - REGISTER_DECODER(AVS, avs); - REGISTER_DECODER(BMP, bmp); - REGISTER_DECODER(CAVS, cavs); - REGISTER_DECODER(CINEPAK, cinepak); - REGISTER_DECODER(CLJR, cljr); - REGISTER_DECODER(CSCD, cscd); - REGISTER_DECODER(CYUV, cyuv); - REGISTER_DECODER(DSICINVIDEO, dsicinvideo); - REGISTER_ENCDEC (DVVIDEO, dvvideo); - REGISTER_DECODER(EIGHTBPS, eightbps); - REGISTER_ENCDEC (FFV1, ffv1); - REGISTER_ENCDEC (FFVHUFF, ffvhuff); - REGISTER_DECODER(FLASHSV, flashsv); - REGISTER_DECODER(FLIC, flic); - REGISTER_ENCDEC (FLV, flv); - REGISTER_DECODER(FOURXM, fourxm); - REGISTER_DECODER(FRAPS, fraps); - REGISTER_ENCDEC (GIF, gif); - REGISTER_ENCDEC (H261, h261); - REGISTER_ENCDEC (H263, h263); - REGISTER_DECODER(H263I, h263i); - REGISTER_ENCODER(H263P, h263p); - REGISTER_DECODER(H264, h264); - REGISTER_ENCDEC (HUFFYUV, huffyuv); - REGISTER_DECODER(IDCIN, idcin); - REGISTER_DECODER(INDEO2, indeo2); - REGISTER_DECODER(INDEO3, indeo3); - REGISTER_DECODER(INTERPLAY_VIDEO, interplay_video); - REGISTER_ENCODER(JPEGLS, jpegls); - REGISTER_DECODER(KMVC, kmvc); - REGISTER_ENCODER(LJPEG, ljpeg); - REGISTER_DECODER(LOCO, loco); - REGISTER_DECODER(MDEC, mdec); - REGISTER_ENCDEC (MJPEG, mjpeg); - REGISTER_DECODER(MJPEGB, mjpegb); - REGISTER_DECODER(MMVIDEO, mmvideo); -#ifdef HAVE_XVMC - REGISTER_DECODER(MPEG_XVMC, mpeg_xvmc); -#endif - REGISTER_ENCDEC (MPEG1VIDEO, mpeg1video); - REGISTER_ENCDEC (MPEG2VIDEO, mpeg2video); - REGISTER_ENCDEC (MPEG4, mpeg4); - REGISTER_DECODER(MPEGVIDEO, mpegvideo); - REGISTER_ENCDEC (MSMPEG4V1, msmpeg4v1); - REGISTER_ENCDEC (MSMPEG4V2, msmpeg4v2); - REGISTER_ENCDEC (MSMPEG4V3, msmpeg4v3); - REGISTER_DECODER(MSRLE, msrle); - REGISTER_DECODER(MSVIDEO1, msvideo1); - REGISTER_DECODER(MSZH, mszh); - REGISTER_DECODER(NUV, nuv); - REGISTER_ENCODER(PAM, pam); - REGISTER_ENCODER(PBM, pbm); - REGISTER_ENCODER(PGM, pgm); - REGISTER_ENCODER(PGMYUV, pgmyuv); -#ifdef CONFIG_ZLIB - REGISTER_ENCDEC (PNG, png); -#endif - REGISTER_ENCODER(PPM, ppm); - REGISTER_DECODER(QDRAW, qdraw); - REGISTER_DECODER(QPEG, qpeg); - REGISTER_DECODER(QTRLE, qtrle); - REGISTER_ENCDEC (RAWVIDEO, rawvideo); - REGISTER_DECODER(ROQ, roq); - REGISTER_DECODER(RPZA, rpza); - REGISTER_ENCDEC (RV10, rv10); - REGISTER_ENCDEC (RV20, rv20); - REGISTER_DECODER(SMACKER, smacker); - REGISTER_DECODER(SMC, smc); - REGISTER_ENCDEC (SNOW, snow); - REGISTER_DECODER(SP5X, sp5x); - REGISTER_ENCDEC (SVQ1, svq1); - REGISTER_DECODER(SVQ3, svq3); - REGISTER_DECODER(TARGA, targa); - REGISTER_DECODER(THEORA, theora); - REGISTER_DECODER(TIERTEXSEQVIDEO, tiertexseqvideo); - REGISTER_DECODER(TIFF, tiff); - REGISTER_DECODER(TRUEMOTION1, truemotion1); - REGISTER_DECODER(TRUEMOTION2, truemotion2); - REGISTER_DECODER(TSCC, tscc); - REGISTER_DECODER(ULTI, ulti); - REGISTER_DECODER(VC1, vc1); - REGISTER_DECODER(VCR1, vcr1); - REGISTER_DECODER(VMDVIDEO, vmdvideo); - REGISTER_DECODER(VMNC, vmnc); - REGISTER_DECODER(VP3, vp3); - REGISTER_DECODER(VP5, vp5); - REGISTER_DECODER(VP6, vp6); - REGISTER_DECODER(VP6F, vp6f); - REGISTER_DECODER(VQA, vqa); - REGISTER_ENCDEC (WMV1, wmv1); - REGISTER_ENCDEC (WMV2, wmv2); - REGISTER_DECODER(WMV3, wmv3); - REGISTER_DECODER(WNV1, wnv1); -#ifdef CONFIG_X264 - REGISTER_ENCODER(X264, x264); -#endif - REGISTER_DECODER(XAN_WC3, xan_wc3); - REGISTER_DECODER(XL, xl); -#ifdef CONFIG_XVID - REGISTER_ENCODER(XVID, xvid); -#endif - REGISTER_ENCDEC (ZLIB, zlib); -#ifdef CONFIG_ZLIB - REGISTER_ENCDEC (ZMBV, zmbv); -#endif - - /* audio codecs */ -#ifdef CONFIG_LIBFAAD - REGISTER_DECODER(AAC, aac); - REGISTER_DECODER(MPEG4AAC, mpeg4aac); -#endif -#ifdef CONFIG_LIBA52 - REGISTER_DECODER(AC3, ac3); -#endif - REGISTER_ENCODER(AC3, ac3); - REGISTER_DECODER(ALAC, alac); -#if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED) - REGISTER_ENCDEC (AMR_NB, amr_nb); -#endif -#ifdef CONFIG_AMR_WB - REGISTER_ENCDEC (AMR_WB, amr_wb); -#endif - REGISTER_DECODER(COOK, cook); - REGISTER_DECODER(DSICINAUDIO, dsicinaudio); -#ifdef CONFIG_LIBDTS - REGISTER_DECODER(DTS, dts); -#endif -#ifdef CONFIG_LIBFAAC - REGISTER_ENCODER(FAAC, faac); -#endif - REGISTER_ENCDEC (FLAC, flac); - REGISTER_DECODER(IMC, imc); -#ifdef CONFIG_LIBGSM - REGISTER_ENCDEC (LIBGSM, libgsm); -#endif - REGISTER_DECODER(MACE3, mace3); - REGISTER_DECODER(MACE6, mace6); - REGISTER_ENCDEC (MP2, mp2); - REGISTER_DECODER(MP3, mp3); - REGISTER_DECODER(MP3ADU, mp3adu); -#ifdef CONFIG_LIBMP3LAME - REGISTER_ENCODER(MP3LAME, mp3lame); -#endif - REGISTER_DECODER(MP3ON4, mp3on4); - REGISTER_DECODER(MPC7, mpc7); -#ifdef CONFIG_LIBVORBIS - if (!ENABLE_VORBIS_ENCODER) REGISTER_ENCODER(OGGVORBIS, oggvorbis); - if (!ENABLE_VORBIS_DECODER) REGISTER_DECODER(OGGVORBIS, oggvorbis); -#endif - REGISTER_DECODER(QDM2, qdm2); - REGISTER_DECODER(RA_144, ra_144); - REGISTER_DECODER(RA_288, ra_288); - REGISTER_DECODER(SHORTEN, shorten); - REGISTER_DECODER(SMACKAUD, smackaud); - REGISTER_ENCDEC (SONIC, sonic); - REGISTER_ENCODER(SONIC_LS, sonic_ls); - REGISTER_DECODER(TRUESPEECH, truespeech); - REGISTER_DECODER(TTA, tta); - REGISTER_DECODER(VMDAUDIO, vmdaudio); - REGISTER_ENCDEC (VORBIS, vorbis); - REGISTER_DECODER(WAVPACK, wavpack); - REGISTER_DECODER(WMAV1, wmav1); - REGISTER_DECODER(WMAV2, wmav2); - REGISTER_DECODER(WS_SND1, ws_snd1); - - /* pcm codecs */ - REGISTER_ENCDEC (PCM_ALAW, pcm_alaw); - REGISTER_ENCDEC (PCM_MULAW, pcm_mulaw); - REGISTER_ENCDEC (PCM_S8, pcm_s8); - REGISTER_ENCDEC (PCM_S16BE, pcm_s16be); - REGISTER_ENCDEC (PCM_S16LE, pcm_s16le); - REGISTER_ENCDEC (PCM_S24BE, pcm_s24be); - REGISTER_ENCDEC (PCM_S24DAUD, pcm_s24daud); - REGISTER_ENCDEC (PCM_S24LE, pcm_s24le); - REGISTER_ENCDEC (PCM_S32BE, pcm_s32be); - REGISTER_ENCDEC (PCM_S32LE, pcm_s32le); - REGISTER_ENCDEC (PCM_U8, pcm_u8); - REGISTER_ENCDEC (PCM_U16BE, pcm_u16be); - REGISTER_ENCDEC (PCM_U16LE, pcm_u16le); - REGISTER_ENCDEC (PCM_U24BE, pcm_u24be); - REGISTER_ENCDEC (PCM_U24LE, pcm_u24le); - REGISTER_ENCDEC (PCM_U32BE, pcm_u32be); - REGISTER_ENCDEC (PCM_U32LE, pcm_u32le); - - /* dpcm codecs */ - REGISTER_DECODER(INTERPLAY_DPCM, interplay_dpcm); - REGISTER_DECODER(ROQ_DPCM, roq_dpcm); - REGISTER_DECODER(SOL_DPCM, sol_dpcm); - REGISTER_DECODER(XAN_DPCM, xan_dpcm); - - /* adpcm codecs */ - REGISTER_ENCDEC (ADPCM_4XM, adpcm_4xm); - REGISTER_ENCDEC (ADPCM_ADX, adpcm_adx); - REGISTER_ENCDEC (ADPCM_CT, adpcm_ct); - REGISTER_ENCDEC (ADPCM_EA, adpcm_ea); - REGISTER_ENCDEC (ADPCM_G726, adpcm_g726); - REGISTER_ENCDEC (ADPCM_IMA_DK3, adpcm_ima_dk3); - REGISTER_ENCDEC (ADPCM_IMA_DK4, adpcm_ima_dk4); - REGISTER_ENCDEC (ADPCM_IMA_QT, adpcm_ima_qt); - REGISTER_ENCDEC (ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg); - REGISTER_ENCDEC (ADPCM_IMA_WAV, adpcm_ima_wav); - REGISTER_ENCDEC (ADPCM_IMA_WS, adpcm_ima_ws); - REGISTER_ENCDEC (ADPCM_MS, adpcm_ms); - REGISTER_ENCDEC (ADPCM_SBPRO_2, adpcm_sbpro_2); - REGISTER_ENCDEC (ADPCM_SBPRO_3, adpcm_sbpro_3); - REGISTER_ENCDEC (ADPCM_SBPRO_4, adpcm_sbpro_4); - REGISTER_ENCDEC (ADPCM_SWF, adpcm_swf); - REGISTER_ENCDEC (ADPCM_XA, adpcm_xa); - REGISTER_ENCDEC (ADPCM_YAMAHA, adpcm_yamaha); - - /* subtitles */ - REGISTER_ENCDEC (DVBSUB, dvbsub); - REGISTER_ENCDEC (DVDSUB, dvdsub); - - /* parsers */ - REGISTER_PARSER (AAC, aac); - REGISTER_PARSER (AC3, ac3); - REGISTER_PARSER (CAVSVIDEO, cavsvideo); - REGISTER_PARSER (DVBSUB, dvbsub); - REGISTER_PARSER (DVDSUB, dvdsub); - REGISTER_PARSER (H261, h261); - REGISTER_PARSER (H263, h263); - REGISTER_PARSER (H264, h264); - REGISTER_PARSER (MJPEG, mjpeg); - REGISTER_PARSER (MPEG4VIDEO, mpeg4video); - REGISTER_PARSER (MPEGAUDIO, mpegaudio); - REGISTER_PARSER (MPEGVIDEO, mpegvideo); - REGISTER_PARSER (PNM, pnm); - - /* - av_register_bitstream_filter(&dump_extradata_bsf); - av_register_bitstream_filter(&remove_extradata_bsf); - av_register_bitstream_filter(&noise_bsf); - av_register_bitstream_filter(&mp3_header_compress_bsf); - av_register_bitstream_filter(&mp3_header_decompress_bsf); - av_register_bitstream_filter(&mjpega_dump_header_bsf); - */ -} - -#endif - -void init_once_routine(void) { - pthread_mutex_init(&ffmpeg_lock, NULL); - avcodec_init(); - avcodec_register_all(); -} - -/* - * exported plugin catalog entry - */ - -const plugin_info_t xine_plugin_info[] EXPORTED = { - /* type, API, "name", version, special_info, init_function */ - { PLUGIN_VIDEO_DECODER | PLUGIN_MUST_PRELOAD, 18, "ffmpegvideo", XINE_VERSION_CODE, &dec_info_ffmpeg_video, init_video_plugin }, - { PLUGIN_VIDEO_DECODER, 18, "ffmpeg-wmv8", XINE_VERSION_CODE, &dec_info_ffmpeg_wmv8, init_video_plugin }, - { PLUGIN_VIDEO_DECODER, 18, "ffmpeg-wmv9", XINE_VERSION_CODE, &dec_info_ffmpeg_wmv9, init_video_plugin }, - { PLUGIN_AUDIO_DECODER, 15, "ffmpegaudio", XINE_VERSION_CODE, &dec_info_ffmpeg_audio, init_audio_plugin }, - { PLUGIN_NONE, 0, "", 0, NULL, NULL } -}; diff --git a/src/libffmpeg/xine_decoder.h b/src/libffmpeg/xine_decoder.h deleted file mode 100644 index 879ee3175..000000000 --- a/src/libffmpeg/xine_decoder.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2001-2005 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.h,v 1.7 2006/08/02 07:15:27 tmmm Exp $ - * - */ - -#ifndef HAVE_XINE_DECODER_H -#define HAVE_XINE_DECODER_H - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#ifdef HAVE_FFMPEG -# include -#else -# include "libavcodec/avcodec.h" -#endif - -typedef struct ff_codec_s { - uint32_t type; - enum CodecID id; - const char *name; -} ff_codec_t; - -void *init_audio_plugin (xine_t *xine, void *data); -void *init_video_plugin (xine_t *xine, void *data); - -extern decoder_info_t dec_info_ffmpeg_video; -extern decoder_info_t dec_info_ffmpeg_wmv8; -extern decoder_info_t dec_info_ffmpeg_wmv9; -extern decoder_info_t dec_info_ffmpeg_audio; - -extern pthread_once_t once_control; -void init_once_routine(void); - -extern pthread_mutex_t ffmpeg_lock; - -#endif diff --git a/src/libffmpeg/xine_encoder.c b/src/libffmpeg/xine_encoder.c deleted file mode 100644 index e234b4ddc..000000000 --- a/src/libffmpeg/xine_encoder.c +++ /dev/null @@ -1,334 +0,0 @@ -/* - * Copyright (C) 2000-2004 the xine project - * - * This file is part of xine, a unix 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_encoder.c,v 1.25 2006/07/10 22:08:29 dgp85 Exp $ - */ - -/* mpeg encoders for the dxr3 video out plugin. */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include -#include -#include -#include -#include -#include - -#define LOG_MODULE "dxr3_mpeg_encoder" -/* #define LOG_VERBOSE */ -/* #define LOG */ - -#include "video_out_dxr3.h" - -#ifdef HAVE_FFMPEG -# include -#else -# include "libavcodec/avcodec.h" -#endif - -/* buffer size for encoded mpeg1 stream; will hold one intra frame - * at 640x480 typical sizes are <50 kB. 512 kB should be plenty */ -#define DEFAULT_BUFFER_SIZE 512*1024 - - -/*initialisation function, used by the dxr3 plugin */ -int dxr3_encoder_init(dxr3_driver_t *drv) EXPORTED; - -/* functions required by encoder api */ -static int lavc_on_update_format(dxr3_driver_t *drv, dxr3_frame_t *frame); -static int lavc_on_display_frame(dxr3_driver_t *drv, dxr3_frame_t *frame); -static int lavc_on_unneeded(dxr3_driver_t *drv); - -/*encoder structure*/ -typedef struct lavc_data_s { - encoder_data_t encoder_data; - AVCodecContext *context; /* handle for encoding */ - int width, height; /* width and height of the video frame */ - uint8_t *ffmpeg_buffer; /* lavc buffer */ - AVFrame *picture; /* picture to be encoded */ - uint8_t *out[3]; /* aligned buffer for YV12 data */ - uint8_t *buf; /* unaligned YV12 buffer */ -} lavc_data_t; - - -int dxr3_encoder_init(dxr3_driver_t *drv) -{ - lavc_data_t* this; - avcodec_init(); - - register_avcodec(&mpeg1video_encoder); - lprintf("lavc init , version %x\n", avcodec_version()); - this = xine_xmalloc(sizeof(lavc_data_t)); - if (!this) return 0; - - this->encoder_data.type = ENC_LAVC; - this->encoder_data.on_update_format = lavc_on_update_format; - this->encoder_data.on_frame_copy = NULL; - this->encoder_data.on_display_frame = lavc_on_display_frame; - this->encoder_data.on_unneeded = lavc_on_unneeded; - this->context = 0; - - drv->enc = &this->encoder_data; - return 1; -} - -/* helper function */ -static int lavc_prepare_frame(lavc_data_t *this, dxr3_driver_t *drv, dxr3_frame_t *frame); - -static int lavc_on_update_format(dxr3_driver_t *drv, dxr3_frame_t *frame) -{ - lavc_data_t *this = (lavc_data_t *)drv->enc; - AVCodec *codec; - unsigned char use_quantizer; - - if (this->context) { - avcodec_close(this->context); - free(this->context); - free(this->picture); - this->context = NULL; - this->picture = NULL; - } - - /* if YUY2 and dimensions changed, we need to re-allocate the - * internal YV12 buffer */ - if (frame->vo_frame.format == XINE_IMGFMT_YUY2) { - int image_size = frame->vo_frame.pitches[0] * frame->oheight; - - this->out[0] = xine_xmalloc_aligned(16, image_size * 3/2, - (void *)&this->buf); - this->out[1] = this->out[0] + image_size; - this->out[2] = this->out[1] + image_size/4; - - /* fill with black (yuv 16,128,128) */ - memset(this->out[0], 16, image_size); - memset(this->out[1], 128, image_size/4); - memset(this->out[2], 128, image_size/4); - lprintf("Using YUY2->YV12 conversion\n"); - } - - /* resolution must be a multiple of two */ - if ((frame->vo_frame.pitches[0] % 2 != 0) || (frame->oheight % 2 != 0)) { - xprintf(drv->class->xine, XINE_VERBOSITY_LOG, - "dxr3_mpeg_encoder: lavc only handles video dimensions which are multiples of 2\n"); - return 0; - } - - /* get mpeg codec handle */ - codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO); - if (!codec) { - xprintf(drv->class->xine, XINE_VERBOSITY_LOG, - "dxr3_mpeg_encoder: lavc MPEG1 codec not found\n"); - return 0; - } - lprintf("lavc MPEG1 encoder found.\n"); - - this->width = frame->vo_frame.pitches[0]; - this->height = frame->oheight; - - this->context = avcodec_alloc_context(); - if (!this->context) { - xprintf(drv->class->xine, XINE_VERBOSITY_LOG, - "dxr3_mpeg_encoder: Couldn't start the ffmpeg library\n"); - return 0; - } - this->picture = avcodec_alloc_frame(); - if (!this->picture) { - xprintf(drv->class->xine, XINE_VERBOSITY_LOG, - "dxr3_mpeg_encoder: Couldn't allocate ffmpeg frame\n"); - return 0; - } - - /* mpeg1 encoder only support YUV420P */ - this->context->pix_fmt = PIX_FMT_YUVJ420P; - - /* put sample parameters */ - this->context->bit_rate = drv->class->xine->config->register_range(drv->class->xine->config, - "dxr3.encoding.lavc_bitrate", 10000, 1000, 20000, - _("libavcodec mpeg output bitrate (kbit/s)"), - _("The bitrate the libavcodec mpeg encoder should use for DXR3's encoding mode. " - "Higher values will increase quality and CPU usage.\n" - "This setting is only considered, when constant quality mode is disabled."), 10, NULL, NULL); - this->context->bit_rate *= 1000; /* config in kbit/s, libavcodec wants bit/s */ - - use_quantizer = drv->class->xine->config->register_bool(drv->class->xine->config, - "dxr3.encoding.lavc_quantizer", 1, - _("constant quality mode"), - _("When enabled, libavcodec will use a constant quality mode by dynamically " - "compressing the images based on their complexity. When disabled, libavcodec " - "will use constant bitrate mode."), 10, NULL, NULL); - - if (use_quantizer) { - this->context->qmin = drv->class->xine->config->register_range(drv->class->xine->config, - "dxr3.encoding.lavc_qmin", 1, 1, 10, - _("minimum compression"), - _("The minimum compression to apply to an image in constant quality mode."), - 10, NULL, NULL); - - this->context->qmax = drv->class->xine->config->register_range(drv->class->xine->config, - "dxr3.encoding.lavc_qmax", 2, 1, 20, - _("maximum quantizer"), - _("The maximum compression to apply to an image in constant quality mode."), - 10, NULL, NULL); - } - - lprintf("lavc -> bitrate %d \n", this->context->bit_rate); - - this->context->width = frame->vo_frame.pitches[0]; - this->context->height = frame->oheight; - - this->context->gop_size = 0; /*intra frames only */ - this->context->me_method = ME_ZERO; /*motion estimation type*/ - - this->context->time_base.den = 90000; - if (frame->vo_frame.duration > 90000 / 24) - this->context->time_base.num = 90000 / 24; - else if (frame->vo_frame.duration < 90000 / 60) - this->context->time_base.num = 90000 / 60; - else - this->context->time_base.num = frame->vo_frame.duration; - /* ffmpeg can complain about illegal framerates, but since this seems no - * problem for the DXR3, we just tell ffmpeg to be more lax with */ - this->context->strict_std_compliance = -1; - - /* open avcodec */ - if (avcodec_open(this->context, codec) < 0) { - xprintf(drv->class->xine, XINE_VERBOSITY_LOG, "dxr3_mpeg_encoder: could not open codec\n"); - return 0; - } - lprintf("dxr3_mpeg_encoder: lavc MPEG1 codec opened.\n"); - - if (!this->ffmpeg_buffer) - this->ffmpeg_buffer = (unsigned char *)malloc(DEFAULT_BUFFER_SIZE); /* why allocate more than needed ?! */ - if (!this->ffmpeg_buffer) { - xprintf(drv->class->xine, XINE_VERBOSITY_LOG, - "dxr3_mpeg_encoder: Couldn't allocate temp buffer for mpeg data\n"); - return 0; - } - - return 1; -} - -static int lavc_on_display_frame(dxr3_driver_t *drv, dxr3_frame_t *frame) -{ - int size; - lavc_data_t* this = (lavc_data_t *)drv->enc; - ssize_t written; - - if (frame->vo_frame.bad_frame) return 1; - /* ignore old frames */ - if ((frame->vo_frame.pitches[0] != this->context->width) || (frame->oheight != this->context->height)) { - frame->vo_frame.free(&frame->vo_frame); - lprintf("LAVC ignoring frame !!!\n"); - return 1; - } - - /* prepare frame for conversion, handles YUY2 -> YV12 conversion when necessary */ - lavc_prepare_frame(this, drv, frame); - - /* do the encoding */ - size = avcodec_encode_video(this->context, this->ffmpeg_buffer, DEFAULT_BUFFER_SIZE, this->picture); - - frame->vo_frame.free(&frame->vo_frame); - - if (size < 0) { - xprintf(drv->class->xine, XINE_VERBOSITY_LOG, - "dxr3_mpeg_encoder: encoding failed\n"); - return 0; - } - - written = write(drv->fd_video, this->ffmpeg_buffer, size); - if (written < 0) { - xprintf(drv->class->xine, XINE_VERBOSITY_LOG, - "dxr3_mpeg_encoder: video device write failed (%s)\n", strerror(errno)); - return 0; - } - if (written != size) - xprintf(drv->class->xine, XINE_VERBOSITY_LOG, - "dxr3_mpeg_encoder: Could only write %zd of %d mpeg bytes.\n", written, size); - return 1; -} - -static int lavc_on_unneeded(dxr3_driver_t *drv) -{ - lavc_data_t *this = (lavc_data_t *)drv->enc; - lprintf("flushing buffers\n"); - if (this->context) { - avcodec_close(this->context); - free(this->context); - free(this->picture); - this->context = NULL; - this->picture = NULL; - } - return 1; -} - -static int lavc_prepare_frame(lavc_data_t *this, dxr3_driver_t *drv, dxr3_frame_t *frame) -{ - int i, j, w2; - uint8_t *yuy2; - - if (frame->vo_frame.bad_frame) return 1; - - if (frame->vo_frame.format == XINE_IMGFMT_YUY2) { - /* need YUY2->YV12 conversion */ - if (!(this->out[0] && this->out[1] && this->out[2]) ) { - lprintf("Internal YV12 buffer not created.\n"); - return 0; - } - this->picture->data[0] = this->out[0] + frame->vo_frame.pitches[0] * drv->top_bar; /* y */ - this->picture->data[1] = this->out[1] + (frame->vo_frame.pitches[0] / 2) * (drv->top_bar / 2); /* u */ - this->picture->data[2] = this->out[2] + (frame->vo_frame.pitches[0] / 2) * (drv->top_bar / 2); /* v */ - yuy2 = frame->vo_frame.base[0]; - w2 = frame->vo_frame.pitches[0] / 2; - for (i = 0; i < frame->vo_frame.height; i += 2) { - for (j = 0; j < w2; j++) { - /* packed YUV 422 is: Y[i] U[i] Y[i+1] V[i] */ - *(this->picture->data[0]++) = *(yuy2++); - *(this->picture->data[1]++) = *(yuy2++); - *(this->picture->data[0]++) = *(yuy2++); - *(this->picture->data[2]++) = *(yuy2++); - } - /* down sampling */ - for (j = 0; j < w2; j++) { - /* skip every second line for U and V */ - *(this->picture->data[0]++) = *(yuy2++); - yuy2++; - *(this->picture->data[0]++) = *(yuy2++); - yuy2++; - } - } - /* reset for encoder */ - this->picture->data[0] = this->out[0]; - this->picture->data[1] = this->out[1]; - this->picture->data[2] = this->out[2]; - } - else { /* YV12 **/ - this->picture->data[0] = frame->real_base[0]; - this->picture->data[1] = frame->real_base[1]; - this->picture->data[2] = frame->real_base[2]; - } - this->picture->linesize[0] = this->context->width; - this->picture->linesize[1] = this->context->width / 2; - this->picture->linesize[2] = this->context->width / 2; - return 1; -} -- cgit v1.2.3