From cfa571253a6f7ac49736e2d0042c120571ddf2a2 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 12 Jul 2013 22:52:10 +0200 Subject: demux_rawvideo/demux_rawaudio: move both demuxers to demux_raw.c This allows them to share some trivial code. Both demuxers are still separate from user perspective. --- Makefile | 3 +- demux/demux_raw.c | 249 +++++++++++++++++++++++++++++++++++++++++++++++++ demux/demux_rawaudio.c | 122 ------------------------ demux/demux_rawvideo.c | 178 ----------------------------------- 4 files changed, 250 insertions(+), 302 deletions(-) create mode 100644 demux/demux_raw.c delete mode 100644 demux/demux_rawaudio.c delete mode 100644 demux/demux_rawvideo.c diff --git a/Makefile b/Makefile index a1de600dcc..ad4728270c 100644 --- a/Makefile +++ b/Makefile @@ -193,8 +193,7 @@ SOURCES = talloc.c \ demux/demux_mf.c \ demux/demux_mkv.c \ demux/demux_subreader.c \ - demux/demux_rawaudio.c \ - demux/demux_rawvideo.c \ + demux/demux_raw.c \ demux/ebml.c \ demux/mf.c \ osdep/io.c \ diff --git a/demux/demux_raw.c b/demux/demux_raw.c new file mode 100644 index 0000000000..1b93d2c23e --- /dev/null +++ b/demux/demux_raw.c @@ -0,0 +1,249 @@ +/* + * This file is part of MPlayer. + * + * MPlayer 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. + * + * MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#include +#include +#include +#include + +#include "core/m_option.h" + +#include "stream/stream.h" +#include "demux.h" +#include "stheader.h" +#include "audio/format.h" + +#include "video/img_format.h" +#include "video/img_fourcc.h" + +struct priv { + int frame_size; + double frame_rate; +}; + +static struct mp_chmap channels = MP_CHMAP_INIT_STEREO; +static int samplerate = 44100; +static int aformat = AF_FORMAT_S16_NE; + +const m_option_t demux_rawaudio_opts[] = { + { "channels", &channels, &m_option_type_chmap, CONF_MIN, 1 }, + { "rate", &samplerate, CONF_TYPE_INT,CONF_RANGE,1000,8*48000, NULL }, + { "format", &aformat, CONF_TYPE_AFMT, 0, 0, 0, NULL }, + {NULL, NULL, 0, 0, 0, 0, NULL} +}; + +static int vformat = MP_FOURCC_I420; +static int mp_format; +static char *codec; +static int width = 0; +static int height = 0; +static float fps = 25; +static int imgsize=0; + +const m_option_t demux_rawvideo_opts[] = { + // size: + { "w", &width, CONF_TYPE_INT,CONF_RANGE,1,8192, NULL }, + { "h", &height, CONF_TYPE_INT,CONF_RANGE,1,8192, NULL }, + // format: + { "format", &vformat, CONF_TYPE_FOURCC, 0, 0 , 0, NULL }, + { "mp-format", &mp_format, CONF_TYPE_IMGFMT, 0, 0 , 0, NULL }, + { "codec", &codec, CONF_TYPE_STRING, 0, 0 , 0, NULL }, + // misc: + { "fps", &fps, CONF_TYPE_FLOAT,CONF_RANGE,0.001,1000, NULL }, + { "size", &imgsize, CONF_TYPE_INT, CONF_RANGE, 1 , 8192*8192*4, NULL }, + + {NULL, NULL, 0, 0, 0, 0, NULL} +}; + +static int demux_rawaudio_open(demuxer_t* demuxer, enum demux_check check) +{ + struct sh_stream *sh; + sh_audio_t* sh_audio; + WAVEFORMATEX* w; + + if (check != DEMUX_CHECK_REQUEST && check != DEMUX_CHECK_FORCE) + return -1; + + if ((aformat & AF_FORMAT_SPECIAL_MASK) != 0) + return -1; + + sh = new_sh_stream(demuxer, STREAM_AUDIO); + sh_audio = sh->audio; + sh_audio->gsh->codec = "mp-pcm"; + sh_audio->format = aformat; + sh_audio->wf = w = malloc(sizeof(*w)); + w->wFormatTag = 0; + sh_audio->channels = channels; + w->nChannels = sh_audio->channels.num; + w->nSamplesPerSec = sh_audio->samplerate = samplerate; + int samplesize = (af_fmt2bits(aformat) + 7) / 8; + w->nAvgBytesPerSec = samplerate * samplesize * w->nChannels; + w->nBlockAlign = w->nChannels * samplesize; + w->wBitsPerSample = 8 * samplesize; + w->cbSize = 0; + + demuxer->movi_start = demuxer->stream->start_pos; + demuxer->movi_end = demuxer->stream->end_pos; + + struct priv *p = talloc_ptrtype(demuxer, p); + demuxer->priv = p; + *p = (struct priv) { + .frame_size = samplesize * sh_audio->channels.num, + .frame_rate = samplerate, + }; + + return 0; +} + +static int demux_rawvideo_open(demuxer_t* demuxer, enum demux_check check) +{ + struct sh_stream *sh; + sh_video_t* sh_video; + + if (check != DEMUX_CHECK_REQUEST && check != DEMUX_CHECK_FORCE) + return -1; + + if(!width || !height){ + mp_msg(MSGT_DEMUX,MSGL_ERR,"rawvideo: width or height not specified!\n"); + return -1; + } + + const char *decoder = "rawvideo"; + int imgfmt = vformat; + if (mp_format) { + decoder = "mp-rawvideo"; + imgfmt = mp_format; + if (!imgsize) { + struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(mp_format); + for (int p = 0; p < desc.num_planes; p++) { + imgsize += ((width >> desc.xs[p]) * (height >> desc.ys[p]) * + desc.bpp[p] + 7) / 8; + } + } + } else if (codec && codec[0]) { + decoder = talloc_strdup(demuxer, codec); + } + + if (!imgsize) { + int bpp = 0; + switch(vformat){ + case MP_FOURCC_I420: case MP_FOURCC_IYUV: + case MP_FOURCC_NV12: case MP_FOURCC_NV21: + case MP_FOURCC_HM12: + case MP_FOURCC_YV12: + bpp = 12; + break; + case MP_FOURCC_RGB12: case MP_FOURCC_BGR12: + case MP_FOURCC_RGB15: case MP_FOURCC_BGR15: + case MP_FOURCC_RGB16: case MP_FOURCC_BGR16: + case MP_FOURCC_YUY2: case MP_FOURCC_UYVY: + bpp = 16; + break; + case MP_FOURCC_RGB8: case MP_FOURCC_BGR8: + case MP_FOURCC_Y800: case MP_FOURCC_Y8: + bpp = 8; + break; + case MP_FOURCC_RGB24: case MP_FOURCC_BGR24: + bpp = 24; + break; + case MP_FOURCC_RGB32: case MP_FOURCC_BGR32: + bpp = 32; + break; + } + if (!bpp) { + mp_msg(MSGT_DEMUX,MSGL_ERR,"rawvideo: img size not specified and unknown format!\n"); + return -1; + } + imgsize = width * height * bpp / 8; + } + + sh = new_sh_stream(demuxer, STREAM_VIDEO); + sh_video = sh->video; + sh_video->gsh->codec=decoder; + sh_video->format=imgfmt; + sh_video->fps=fps; + sh_video->disp_w=width; + sh_video->disp_h=height; + sh_video->i_bps=fps*imgsize; + + demuxer->movi_start = demuxer->stream->start_pos; + demuxer->movi_end = demuxer->stream->end_pos; + + struct priv *p = talloc_ptrtype(demuxer, p); + demuxer->priv = p; + *p = (struct priv) { + .frame_size = imgsize, + .frame_rate = fps, + }; + + return 0; +} + +static int raw_fill_buffer(demuxer_t* demuxer) +{ + struct priv *p = demuxer->priv; + + if (demuxer->stream->eof) + return 0; + + struct demux_packet *dp = new_demux_packet(p->frame_size); + dp->pos = stream_tell(demuxer->stream) - demuxer->movi_start; + dp->pts = (dp->pos / p->frame_size) / p->frame_rate; + + int len = stream_read(demuxer->stream, dp->buffer, dp->len); + resize_demux_packet(dp, len); + demuxer_add_packet(demuxer, demuxer->streams[0], dp); + + return 1; +} + +static void raw_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){ + struct priv *p = demuxer->priv; + stream_t* s = demuxer->stream; + stream_update_size(s); + int64_t start = s->start_pos; + int64_t end = s->end_pos; + int64_t pos = (flags & SEEK_ABSOLUTE) ? start : stream_tell(s); + if (flags & SEEK_FACTOR) + pos += (end - start) * rel_seek_secs; + else + pos += rel_seek_secs * p->frame_rate * p->frame_size; + if(pos < 0) + pos = 0; + if (end && pos > end) + pos = end; + stream_seek(s, (pos / p->frame_size) * p->frame_size); +} + +const demuxer_desc_t demuxer_desc_rawaudio = { + .name = "rawaudio", + .desc = "Uncompressed audio", + .open = demux_rawaudio_open, + .fill_buffer = raw_fill_buffer, + .seek = raw_seek, +}; + +const demuxer_desc_t demuxer_desc_rawvideo = { + .name = "rawvideo", + .desc = "Uncompressed video", + .open = demux_rawvideo_open, + .fill_buffer = raw_fill_buffer, + .seek = raw_seek, +}; diff --git a/demux/demux_rawaudio.c b/demux/demux_rawaudio.c deleted file mode 100644 index 54e7b1b939..0000000000 --- a/demux/demux_rawaudio.c +++ /dev/null @@ -1,122 +0,0 @@ -/* - * This file is part of MPlayer. - * - * MPlayer 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. - * - * MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "core/m_option.h" - -#include "stream/stream.h" -#include "demux.h" -#include "stheader.h" -#include "audio/format.h" - - -static struct mp_chmap channels = MP_CHMAP_INIT_STEREO; -static int samplerate = 44100; -static int format = AF_FORMAT_S16_NE; - -const m_option_t demux_rawaudio_opts[] = { - { "channels", &channels, &m_option_type_chmap, CONF_MIN, 1 }, - { "rate", &samplerate, CONF_TYPE_INT,CONF_RANGE,1000,8*48000, NULL }, - { "format", &format, CONF_TYPE_AFMT, 0, 0, 0, NULL }, - {NULL, NULL, 0, 0, 0, 0, NULL} -}; - - -static int demux_rawaudio_open(demuxer_t* demuxer, enum demux_check check) -{ - struct sh_stream *sh; - sh_audio_t* sh_audio; - WAVEFORMATEX* w; - - if (check != DEMUX_CHECK_REQUEST && check != DEMUX_CHECK_FORCE) - return -1; - - if ((format & AF_FORMAT_SPECIAL_MASK) != 0) - return -1; - - sh = new_sh_stream(demuxer, STREAM_AUDIO); - sh_audio = sh->audio; - sh_audio->gsh->codec = "mp-pcm"; - sh_audio->format = format; - sh_audio->wf = w = malloc(sizeof(*w)); - w->wFormatTag = 0; - sh_audio->channels = channels; - w->nChannels = sh_audio->channels.num; - w->nSamplesPerSec = sh_audio->samplerate = samplerate; - int samplesize = (af_fmt2bits(format) + 7) / 8; - w->nAvgBytesPerSec = samplerate * samplesize * w->nChannels; - w->nBlockAlign = w->nChannels * samplesize; - w->wBitsPerSample = 8 * samplesize; - w->cbSize = 0; - - demuxer->movi_start = demuxer->stream->start_pos; - demuxer->movi_end = demuxer->stream->end_pos; - - return 0; -} - -static int demux_rawaudio_fill_buffer(demuxer_t* demuxer) -{ - sh_audio_t* sh_audio = demuxer->streams[0]->audio; - int l = sh_audio->wf->nAvgBytesPerSec; - int64_t spos = stream_tell(demuxer->stream); - demux_packet_t* dp; - - if(demuxer->stream->eof) - return 0; - - dp = new_demux_packet(l); - dp->pts = (spos - demuxer->movi_start) / (float)(sh_audio->wf->nAvgBytesPerSec); - dp->pos = (spos - demuxer->movi_start); - - l = stream_read(demuxer->stream,dp->buffer,l); - resize_demux_packet(dp, l); - demuxer_add_packet(demuxer, demuxer->streams[0], dp); - - return 1; -} - -static void demux_rawaudio_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){ - stream_t* s = demuxer->stream; - sh_audio_t* sh_audio = demuxer->streams[0]->audio; - int64_t base,pos; - - base = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : stream_tell(s); - if(flags & SEEK_FACTOR) - pos = base + ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs); - else - pos = base + (rel_seek_secs*sh_audio->i_bps); - - pos -= (pos % (sh_audio->channels.num * sh_audio->samplesize) ); - stream_seek(s,pos); -// printf("demux_rawaudio: streamtell=%d\n",(int)stream_tell(demuxer->stream)); -} - -const demuxer_desc_t demuxer_desc_rawaudio = { - .name = "rawaudio", - .desc = "Uncompressed audio", - .fill_buffer = demux_rawaudio_fill_buffer, - .open = demux_rawaudio_open, - .seek = demux_rawaudio_seek, -}; diff --git a/demux/demux_rawvideo.c b/demux/demux_rawvideo.c deleted file mode 100644 index 5d556f6be0..0000000000 --- a/demux/demux_rawvideo.c +++ /dev/null @@ -1,178 +0,0 @@ -/* - * This file is part of MPlayer. - * - * MPlayer 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. - * - * MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "core/m_option.h" - -#include "stream/stream.h" -#include "demux.h" -#include "stheader.h" - -#include "video/img_format.h" -#include "video/img_fourcc.h" - -static int format = MP_FOURCC_I420; -static int mp_format; -static char *codec; -static int width = 0; -static int height = 0; -static float fps = 25; -static int imgsize=0; - -const m_option_t demux_rawvideo_opts[] = { - // size: - { "w", &width, CONF_TYPE_INT,CONF_RANGE,1,8192, NULL }, - { "h", &height, CONF_TYPE_INT,CONF_RANGE,1,8192, NULL }, - // format: - { "format", &format, CONF_TYPE_FOURCC, 0, 0 , 0, NULL }, - { "mp-format", &mp_format, CONF_TYPE_IMGFMT, 0, 0 , 0, NULL }, - { "codec", &codec, CONF_TYPE_STRING, 0, 0 , 0, NULL }, - // misc: - { "fps", &fps, CONF_TYPE_FLOAT,CONF_RANGE,0.001,1000, NULL }, - { "size", &imgsize, CONF_TYPE_INT, CONF_RANGE, 1 , 8192*8192*4, NULL }, - - {NULL, NULL, 0, 0, 0, 0, NULL} -}; - - -static int demux_rawvideo_open(demuxer_t* demuxer, enum demux_check check) -{ - struct sh_stream *sh; - sh_video_t* sh_video; - - if (check != DEMUX_CHECK_REQUEST && check != DEMUX_CHECK_FORCE) - return -1; - - if(!width || !height){ - mp_msg(MSGT_DEMUX,MSGL_ERR,"rawvideo: width or height not specified!\n"); - return -1; - } - - const char *decoder = "rawvideo"; - int imgfmt = format; - if (mp_format) { - decoder = "mp-rawvideo"; - imgfmt = mp_format; - if (!imgsize) { - struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(mp_format); - for (int p = 0; p < desc.num_planes; p++) { - imgsize += ((width >> desc.xs[p]) * (height >> desc.ys[p]) * - desc.bpp[p] + 7) / 8; - } - } - } else if (codec && codec[0]) { - decoder = talloc_strdup(demuxer, codec); - } - - if (!imgsize) { - int bpp = 0; - switch(format){ - case MP_FOURCC_I420: case MP_FOURCC_IYUV: - case MP_FOURCC_NV12: case MP_FOURCC_NV21: - case MP_FOURCC_HM12: - case MP_FOURCC_YV12: - bpp = 12; - break; - case MP_FOURCC_RGB12: case MP_FOURCC_BGR12: - case MP_FOURCC_RGB15: case MP_FOURCC_BGR15: - case MP_FOURCC_RGB16: case MP_FOURCC_BGR16: - case MP_FOURCC_YUY2: case MP_FOURCC_UYVY: - bpp = 16; - break; - case MP_FOURCC_RGB8: case MP_FOURCC_BGR8: - case MP_FOURCC_Y800: case MP_FOURCC_Y8: - bpp = 8; - break; - case MP_FOURCC_RGB24: case MP_FOURCC_BGR24: - bpp = 24; - break; - case MP_FOURCC_RGB32: case MP_FOURCC_BGR32: - bpp = 32; - break; - } - if (!bpp) { - mp_msg(MSGT_DEMUX,MSGL_ERR,"rawvideo: img size not specified and unknown format!\n"); - return -1; - } - imgsize = width * height * bpp / 8; - } - - sh = new_sh_stream(demuxer, STREAM_VIDEO); - sh_video = sh->video; - sh_video->gsh->codec=decoder; - sh_video->format=imgfmt; - sh_video->fps=fps; - sh_video->disp_w=width; - sh_video->disp_h=height; - sh_video->i_bps=fps*imgsize; - - demuxer->movi_start = demuxer->stream->start_pos; - demuxer->movi_end = demuxer->stream->end_pos; - - return 0; -} - -static int demux_rawvideo_fill_buffer(demuxer_t* demuxer) -{ - int64_t spos = stream_tell(demuxer->stream); - demux_packet_t* dp; - int size; - - if(demuxer->stream->eof) - return 0; - - dp = new_demux_packet(imgsize); - dp->pos = (spos - demuxer->movi_start); - dp->pts = dp->pos / (float)(imgsize) / fps; - - size = stream_read(demuxer->stream, dp->buffer, imgsize); - resize_demux_packet(dp, size); - demuxer_add_packet(demuxer, demuxer->streams[0], dp); - - return 1; -} - -static void demux_rawvideo_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){ - stream_t* s = demuxer->stream; - int64_t pos; - - pos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : stream_tell(s); - if(flags & SEEK_FACTOR) - pos += ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs); - else - pos += (rel_seek_secs*fps*imgsize); - if(pos < 0) pos = 0; - if(demuxer->movi_end && pos > demuxer->movi_end) pos = (demuxer->movi_end-imgsize); - pos/=imgsize; - stream_seek(s,pos*imgsize); -} - - -const demuxer_desc_t demuxer_desc_rawvideo = { - .name = "rawvideo", - .desc = "Uncompressed video", - .fill_buffer = demux_rawvideo_fill_buffer, - .open = demux_rawvideo_open, - .seek = demux_rawvideo_seek, -}; -- cgit v1.2.3