Merge svn changes up to r26979
[mplayer.git] / libmpdemux / demux_rawdv.c
blob503a906fe1b33ad14cbd0476eae138036b9d24bf
1 /*
2 raw dv file parser for MPlayer
3 by Alexander Neundorf <neundorf@kde.org>
4 based on the fli demuxer
6 LGPL
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
13 #include "config.h"
15 #include "mp_msg.h"
16 #include "help_mp.h"
18 #include "stream/stream.h"
19 #include "demuxer.h"
20 #include "stheader.h"
22 #include <libdv/dv.h>
23 #include <libdv/dv_types.h>
25 #define DV_PAL_FRAME_SIZE 144000
26 #define DV_NTSC_FRAME_SIZE 122000
28 typedef struct
30 int current_frame;
31 int frame_size;
32 off_t current_filepos;
33 int frame_number;
34 dv_decoder_t *decoder;
35 } rawdv_frames_t;
37 static void demux_seek_rawdv(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags)
39 rawdv_frames_t *frames = (rawdv_frames_t *)demuxer->priv;
40 sh_video_t *sh_video = demuxer->video->sh;
41 off_t newpos=(flags&SEEK_ABSOLUTE)?0:frames->current_frame;
42 if(flags&SEEK_FACTOR)
44 // float 0..1
45 newpos+=rel_seek_secs*frames->frame_number;
47 else
49 // secs
50 newpos+=rel_seek_secs*sh_video->fps;
52 if(newpos<0)
53 newpos=0;
54 else if(newpos>frames->frame_number)
55 newpos=frames->frame_number;
56 frames->current_frame=newpos;
57 frames->current_filepos=newpos*frames->frame_size;
60 static int rawdv_check_file(demuxer_t *demuxer)
62 unsigned char tmp_buffer[DV_PAL_FRAME_SIZE];
63 int bytes_read=0;
64 int result=0;
65 dv_decoder_t *td;
67 mp_msg(MSGT_DEMUX,MSGL_V,"Checking for DV\n");
69 bytes_read=stream_read(demuxer->stream,tmp_buffer,DV_PAL_FRAME_SIZE);
70 if ((bytes_read!=DV_PAL_FRAME_SIZE) && (bytes_read!=DV_NTSC_FRAME_SIZE))
71 return 0;
73 td=dv_decoder_new(TRUE,TRUE,FALSE);
74 if (!td)
75 return 0;
77 td->quality=DV_QUALITY_BEST;
78 result=dv_parse_header(td, tmp_buffer);
79 if (result<0)
80 return 0;
82 if ((( td->num_dif_seqs==10) || (td->num_dif_seqs==12))
83 && (td->width==720)
84 && ((td->height==576) || (td->height==480)))
85 result=1;
86 dv_decoder_free(td);
87 if (result)
88 return DEMUXER_TYPE_RAWDV;
89 else
90 return 0;
93 // return value:
94 // 0 = EOF or no stream found
95 // 1 = successfully read a packet
96 static int demux_rawdv_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds)
98 rawdv_frames_t *frames = (rawdv_frames_t *)demuxer->priv;
99 demux_packet_t* dp_video=NULL;
100 sh_video_t *sh_video = demuxer->video->sh;
101 int bytes_read=0;
102 // fprintf(stderr,"demux_rawdv_fill_buffer() seek to %qu, size: %d\n",frames->current_filepos,frames->frame_size);
103 // fetch the frame from the file
104 // first, position the file properly since ds_read_packet() doesn't
105 // seem to do it, even though it takes a file offset as a parameter
106 stream_seek(demuxer->stream, frames->current_filepos);
108 dp_video=new_demux_packet(frames->frame_size);
109 bytes_read=stream_read(demuxer->stream,dp_video->buffer,frames->frame_size);
110 if (bytes_read<frames->frame_size)
111 return 0;
112 dp_video->pts=frames->current_frame/sh_video->fps;
113 dp_video->pos=frames->current_filepos;
114 dp_video->flags=0;
116 if (demuxer->audio && demuxer->audio->id>=-1)
118 demux_packet_t* dp_audio=clone_demux_packet(dp_video);
119 ds_add_packet(demuxer->audio,dp_audio);
121 ds_add_packet(demuxer->video,dp_video);
122 // get the next frame ready
123 frames->current_filepos+=frames->frame_size;
124 frames->current_frame++;
125 // fprintf(stderr," audio->packs: %d , video->packs: %d \n",demuxer->audio->packs, demuxer->video->packs);
126 return 1;
129 static demuxer_t* demux_open_rawdv(demuxer_t* demuxer)
131 unsigned char dv_frame[DV_PAL_FRAME_SIZE];
132 sh_video_t *sh_video = NULL;
133 rawdv_frames_t *frames = malloc(sizeof(rawdv_frames_t));
134 dv_decoder_t *dv_decoder=NULL;
136 mp_msg(MSGT_DEMUXER,MSGL_V,"demux_open_rawdv() end_pos %"PRId64"\n",(int64_t)demuxer->stream->end_pos);
138 // go back to the beginning
139 stream_reset(demuxer->stream);
140 stream_seek(demuxer->stream, 0);
142 //get the first frame
143 stream_read(demuxer->stream, dv_frame, DV_PAL_FRAME_SIZE);
145 //read params from this frame
146 dv_decoder=dv_decoder_new(TRUE,TRUE,FALSE);
147 dv_decoder->quality=DV_QUALITY_BEST;
149 if (dv_parse_header(dv_decoder, dv_frame) == -1)
150 return NULL;
152 // create a new video stream header
153 sh_video = new_sh_video(demuxer, 0);
154 if (!sh_video)
155 return NULL;
157 // make sure the demuxer knows about the new video stream header
158 // (even though new_sh_video() ought to take care of it)
159 demuxer->seekable = 1;
160 demuxer->video->sh = sh_video;
162 // make sure that the video demuxer stream header knows about its
163 // parent video demuxer stream (this is getting wacky), or else
164 // video_read_properties() will choke
165 sh_video->ds = demuxer->video;
167 // custom fourcc for internal MPlayer use
168 // sh_video->format = mmioFOURCC('R', 'A', 'D', 'V');
169 sh_video->format = mmioFOURCC('D', 'V', 'S', 'D');
171 sh_video->disp_w = dv_decoder->width;
172 sh_video->disp_h = dv_decoder->height;
173 mp_msg(MSGT_DEMUXER,MSGL_V,"demux_open_rawdv() frame_size: %d w: %d h: %d dif_seq: %d system: %d\n",dv_decoder->frame_size,dv_decoder->width, dv_decoder->height,dv_decoder->num_dif_seqs,dv_decoder->system);
175 sh_video->fps= (dv_decoder->system==e_dv_system_525_60?29.97:25);
176 sh_video->frametime = 1.0/sh_video->fps;
178 // emulate BITMAPINFOHEADER for win32 decoders:
179 sh_video->bih=malloc(sizeof(BITMAPINFOHEADER));
180 memset(sh_video->bih,0,sizeof(BITMAPINFOHEADER));
181 sh_video->bih->biSize=40;
182 sh_video->bih->biWidth = dv_decoder->width;
183 sh_video->bih->biHeight = dv_decoder->height;
184 sh_video->bih->biPlanes=1;
185 sh_video->bih->biBitCount=24;
186 sh_video->bih->biCompression=sh_video->format; // "DVSD"
187 sh_video->bih->biSizeImage=sh_video->bih->biWidth*sh_video->bih->biHeight*3;
190 frames->current_filepos=0;
191 frames->current_frame=0;
192 frames->frame_size=dv_decoder->frame_size;
193 frames->frame_number=demuxer->stream->end_pos/frames->frame_size;
195 mp_msg(MSGT_DEMUXER,MSGL_V,"demux_open_rawdv() seek to %qu, size: %d, dv_dec->frame_size: %d\n",frames->current_filepos,frames->frame_size, dv_decoder->frame_size);
196 if (dv_decoder->audio != NULL && demuxer->audio->id>=-1){
197 sh_audio_t *sh_audio = new_sh_audio(demuxer, 0);
198 demuxer->audio->id = 0;
199 demuxer->audio->sh = sh_audio;
200 sh_audio->ds = demuxer->audio;
201 mp_msg(MSGT_DEMUXER,MSGL_V,"demux_open_rawdv() chan: %d samplerate: %d\n",dv_decoder->audio->num_channels,dv_decoder->audio->frequency );
202 // custom fourcc for internal MPlayer use
203 sh_audio->format = mmioFOURCC('R', 'A', 'D', 'V');
205 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
206 memset(sh_audio->wf, 0, sizeof(WAVEFORMATEX));
207 sh_audio->wf->wFormatTag = sh_audio->format;
208 sh_audio->wf->nChannels = dv_decoder->audio->num_channels;
209 sh_audio->wf->wBitsPerSample = 16;
210 sh_audio->wf->nSamplesPerSec = dv_decoder->audio->frequency;
211 // info about the input stream:
212 sh_audio->wf->nAvgBytesPerSec = sh_video->fps*dv_decoder->frame_size;
213 sh_audio->wf->nBlockAlign = dv_decoder->frame_size;
215 // sh_audio->context=(void*)dv_decoder;
217 stream_reset(demuxer->stream);
218 stream_seek(demuxer->stream, 0);
219 dv_decoder_free(dv_decoder); //we keep this in the context of both stream headers
220 demuxer->priv=frames;
221 return demuxer;
224 static void demux_close_rawdv(demuxer_t* demuxer)
226 rawdv_frames_t *frames = (rawdv_frames_t *)demuxer->priv;
228 if(frames==0)
229 return;
230 free(frames);
233 static int demux_rawdv_control(demuxer_t *demuxer,int cmd, void *arg) {
234 rawdv_frames_t *frames = (rawdv_frames_t *)demuxer->priv;
235 sh_video_t *sh_video=demuxer->video->sh;
237 switch(cmd) {
238 case DEMUXER_CTRL_GET_TIME_LENGTH:
239 *((double *)arg)=(double)frames->frame_number / sh_video->fps;
240 return DEMUXER_CTRL_OK;
242 case DEMUXER_CTRL_GET_PERCENT_POS:
243 *((int *)arg)=(int)(frames->current_frame * 100. / frames->frame_number);
244 return DEMUXER_CTRL_OK;
246 default:
247 return DEMUXER_CTRL_NOTIMPL;
252 const demuxer_desc_t demuxer_desc_rawdv = {
253 "Raw DV demuxer",
254 "rawdv",
255 "RAWDV",
256 "Alexander Neundorf",
258 DEMUXER_TYPE_RAWDV,
259 0, // unsafe autodetect
260 rawdv_check_file,
261 demux_rawdv_fill_buffer,
262 demux_open_rawdv,
263 demux_close_rawdv,
264 demux_seek_rawdv,
265 demux_rawdv_control