stream/tv: move new_handle() function from header to tv.c
[mplayer.git] / libmpdemux / demux_rawvideo.c
blobe84caafd019049d1a883769d7815941141d9c91d
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
26 #include "m_option.h"
28 #include "stream/stream.h"
29 #include "demuxer.h"
30 #include "stheader.h"
32 #include "libmpcodecs/img_format.h"
34 static int format = IMGFMT_I420;
35 static int size_id = 0;
36 static int width = 0;
37 static int height = 0;
38 static float fps = 25;
39 static int imgsize=0;
41 const m_option_t demux_rawvideo_opts[] = {
42 // size:
43 { "w", &width, CONF_TYPE_INT,CONF_RANGE,1,8192, NULL },
44 { "h", &height, CONF_TYPE_INT,CONF_RANGE,1,8192, NULL },
45 { "sqcif", &size_id, CONF_TYPE_FLAG,0,0,1, NULL },
46 { "qcif", &size_id, CONF_TYPE_FLAG,0,0,2, NULL },
47 { "cif", &size_id, CONF_TYPE_FLAG,0,0,3, NULL },
48 { "4cif", &size_id, CONF_TYPE_FLAG,0,0,4, NULL },
49 { "pal", &size_id, CONF_TYPE_FLAG,0,0,5, NULL },
50 { "ntsc", &size_id, CONF_TYPE_FLAG,0,0,6, NULL },
51 { "16cif", &size_id, CONF_TYPE_FLAG,0,0,7, NULL },
52 { "sif", &size_id, CONF_TYPE_FLAG,0,0,8, NULL },
53 // format:
54 { "format", &format, CONF_TYPE_IMGFMT, 0, 0 , 0, NULL },
55 // below options are obsolete
56 { "i420", &format, CONF_TYPE_FLAG, 0, 0 , IMGFMT_I420, NULL },
57 { "yv12", &format, CONF_TYPE_FLAG, 0, 0 , IMGFMT_YV12, NULL },
58 { "nv12", &format, CONF_TYPE_FLAG, 0, 0 , IMGFMT_NV12, NULL },
59 { "hm12", &format, CONF_TYPE_FLAG, 0, 0 , IMGFMT_HM12, NULL },
60 { "yuy2", &format, CONF_TYPE_FLAG, 0, 0 , IMGFMT_YUY2, NULL },
61 { "uyvy", &format, CONF_TYPE_FLAG, 0, 0 , IMGFMT_UYVY, NULL },
62 { "y8", &format, CONF_TYPE_FLAG, 0, 0 , IMGFMT_Y8, NULL },
63 // misc:
64 { "fps", &fps, CONF_TYPE_FLOAT,CONF_RANGE,0.001,1000, NULL },
65 { "size", &imgsize, CONF_TYPE_INT, CONF_RANGE, 1 , 8192*8192*4, NULL },
67 {NULL, NULL, 0, 0, 0, 0, NULL}
71 static demuxer_t* demux_rawvideo_open(demuxer_t* demuxer) {
72 sh_video_t* sh_video;
74 switch(size_id){
75 case 1: width=128; height=96; break;
76 case 2: width=176; height=144; break;
77 case 3: width=352; height=288; break;
78 case 4: width=704; height=576; break;
79 case 5: width=720; height=576; break;
80 case 6: width=720; height=480; break;
81 case 7: width=1408;height=1152;break;
82 case 8: width=352; height=240; break;
84 if(!width || !height){
85 mp_msg(MSGT_DEMUX,MSGL_ERR,"rawvideo: width or height not specified!\n");
86 return 0;
89 if(!imgsize)
90 switch(format){
91 case IMGFMT_I420:
92 case IMGFMT_IYUV:
93 case IMGFMT_NV12:
94 case IMGFMT_HM12:
95 case IMGFMT_YV12: imgsize=width*height+2*(width>>1)*(height>>1);break;
96 case IMGFMT_YUY2:
97 case IMGFMT_UYVY: imgsize=width*height*2;break;
98 case IMGFMT_Y800:
99 case IMGFMT_Y8: imgsize=width*height;break;
100 default:
101 if (IMGFMT_IS_RGB(format))
102 imgsize = width * height * ((IMGFMT_RGB_DEPTH(format) + 7) >> 3);
103 else if (IMGFMT_IS_BGR(format))
104 imgsize = width * height * ((IMGFMT_BGR_DEPTH(format) + 7) >> 3);
105 else {
106 mp_msg(MSGT_DEMUX,MSGL_ERR,"rawvideo: img size not specified and unknown format!\n");
107 return 0;
111 sh_video = new_sh_video(demuxer,0);
112 sh_video->format=format;
113 sh_video->fps=fps;
114 sh_video->frametime=1.0/fps;
115 sh_video->disp_w=width;
116 sh_video->disp_h=height;
117 sh_video->i_bps=fps*imgsize;
119 demuxer->movi_start = demuxer->stream->start_pos;
120 demuxer->movi_end = demuxer->stream->end_pos;
122 demuxer->video->sh = sh_video;
123 sh_video->ds = demuxer->video;
125 return demuxer;
128 static int demux_rawvideo_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds) {
129 sh_video_t* sh = demuxer->video->sh;
130 off_t pos;
131 if(demuxer->stream->eof) return 0;
132 if(ds!=demuxer->video) return 0;
133 pos = stream_tell(demuxer->stream);
134 ds_read_packet(ds,demuxer->stream,imgsize,(pos/imgsize)*sh->frametime,pos,0x10);
135 return 1;
138 static void demux_rawvideo_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
139 stream_t* s = demuxer->stream;
140 sh_video_t* sh_video = demuxer->video->sh;
141 off_t pos;
143 pos = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : stream_tell(s);
144 if(flags & SEEK_FACTOR)
145 pos += ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs);
146 else
147 pos += (rel_seek_secs*sh_video->i_bps);
148 if(pos < 0) pos = 0;
149 if(demuxer->movi_end && pos > demuxer->movi_end) pos = (demuxer->movi_end-imgsize);
150 pos/=imgsize;
151 stream_seek(s,pos*imgsize);
152 //sh_video->timer=pos * sh_video->frametime;
153 demuxer->video->pts = pos * sh_video->frametime;
154 // printf("demux_rawvideo: streamtell=%d\n",(int)stream_tell(demuxer->stream));
158 const demuxer_desc_t demuxer_desc_rawvideo = {
159 "Raw video demuxer",
160 "rawvideo",
161 "rawvideo",
162 "?",
164 DEMUXER_TYPE_RAWVIDEO,
165 0, // no autodetect
166 NULL,
167 demux_rawvideo_fill_buffer,
168 demux_rawvideo_open,
169 NULL,
170 demux_rawvideo_seek,
171 NULL