2531
|
1
|
|
2 #define VCODEC_DIVX4 1
|
|
3
|
|
4 #include <stdio.h>
|
|
5 #include <stdlib.h>
|
|
6 #include <string.h>
|
|
7 #include <signal.h>
|
|
8
|
|
9 #include "mp_msg.h"
|
|
10 #include "help_mp.h"
|
|
11
|
|
12 #include "codec-cfg.h"
|
|
13
|
|
14 #include "stream.h"
|
|
15 #include "demuxer.h"
|
|
16 #include "stheader.h"
|
|
17
|
|
18 #include "aviwrite.h"
|
|
19
|
|
20 #include "libvo/video_out.h"
|
|
21
|
2574
|
22 #include "dec_audio.h"
|
|
23 #include "dec_video.h"
|
|
24
|
2531
|
25 #include <encore2.h>
|
|
26
|
2571
|
27 #include "get_path.c"
|
2531
|
28
|
|
29 //--------------------------
|
|
30
|
|
31 // cache2:
|
|
32 #ifdef USE_STREAM_CACHE
|
|
33 extern int cache_fill_status;
|
|
34 #else
|
|
35 #define cache_fill_status 0
|
|
36 #endif
|
|
37
|
|
38 // AVI demuxer params:
|
|
39 static float c_total=0;
|
|
40 int delay_corrected=1;
|
|
41
|
|
42 char *audio_codec=NULL; // override audio codec
|
|
43 char *video_codec=NULL; // override video codec
|
|
44 int audio_family=-1; // override audio codec family
|
|
45 int video_family=-1; // override video codec family
|
|
46
|
|
47 // audio stream skip/resync functions requires only for seeking.
|
|
48 // (they should be implemented in the audio codec layer)
|
|
49 //void skip_audio_frame(sh_audio_t *sh_audio){}
|
|
50 //void resync_audio_stream(sh_audio_t *sh_audio){}
|
|
51
|
|
52 int verbose=5; // must be global!
|
|
53
|
|
54 double video_time_usage=0;
|
|
55 double vout_time_usage=0;
|
|
56 static double audio_time_usage=0;
|
|
57 static int total_time_usage_start=0;
|
|
58 static int benchmark=0;
|
|
59
|
|
60 int force_fps=0;
|
|
61
|
|
62 #include "libmpeg2/mpeg2.h"
|
|
63 #include "libmpeg2/mpeg2_internal.h"
|
|
64
|
|
65 static unsigned char* vo_image=NULL;
|
|
66 static unsigned char* vo_image_ptr=NULL;
|
|
67 static int vo_w,vo_h;
|
|
68
|
|
69 static uint32_t draw_slice(uint8_t *src[], int stride[], int w,int h, int x0,int y0){
|
|
70 int y;
|
|
71 // printf("draw_slice %dx%d %d;%d\n",w,h,x,y);
|
|
72
|
|
73 // copy Y:
|
|
74 for(y=0;y<h;y++){
|
|
75 unsigned char* s=src[0]+stride[0]*(y0+y)+x0;
|
|
76 unsigned char* d=vo_image+vo_w*(y0+y)+x0;
|
|
77 memcpy(d,s,w);
|
|
78 }
|
|
79 x0>>=1;y0>>=1;
|
|
80 w>>=1;h>>=1;
|
|
81 // copy U:
|
|
82 for(y=0;y<h;y++){
|
|
83 unsigned char* s=src[1]+stride[1]*(y0+y)+x0;
|
|
84 unsigned char* d=vo_image+vo_w*vo_h+(vo_w>>1)*(y0+y)+x0;
|
|
85 memcpy(d,s,w);
|
|
86 }
|
|
87 // copy V:
|
|
88 for(y=0;y<h;y++){
|
|
89 unsigned char* s=src[1]+stride[1]*(y0+y)+x0;
|
|
90 unsigned char* d=vo_image+vo_w*vo_h+vo_w*vo_h/4+(vo_w>>1)*(y0+y)+x0;
|
|
91 memcpy(d,s,w);
|
|
92 }
|
|
93
|
|
94 }
|
|
95
|
|
96 static uint32_t draw_frame(uint8_t *src[]){
|
|
97 // printf("This function shouldn't be called - report bug!\n");
|
|
98 // later: add YUY2->YV12 conversion here!
|
|
99 vo_image_ptr=src[0];
|
|
100 }
|
|
101
|
|
102 vo_functions_t video_out;
|
|
103
|
|
104 //---------------
|
|
105
|
|
106 static int eof=0;
|
|
107
|
|
108 static void exit_sighandler(int x){
|
|
109 eof=1;
|
|
110 }
|
|
111
|
|
112 int main(int argc,char* argv[]){
|
|
113
|
|
114 stream_t* stream=NULL;
|
|
115 demuxer_t* demuxer=NULL;
|
|
116 demux_stream_t *d_audio=NULL;
|
|
117 demux_stream_t *d_video=NULL;
|
|
118 demux_stream_t *d_dvdsub=NULL;
|
|
119 sh_audio_t *sh_audio=NULL;
|
|
120 sh_video_t *sh_video=NULL;
|
|
121 int file_format=DEMUXER_TYPE_UNKNOWN;
|
|
122 int i;
|
|
123 unsigned int out_fmt;
|
|
124
|
|
125 aviwrite_t* muxer=NULL;
|
|
126 aviwrite_stream_t* mux_a=NULL;
|
|
127 aviwrite_stream_t* mux_v=NULL;
|
|
128 FILE* muxer_f=NULL;
|
|
129
|
|
130 ENC_PARAM enc_param;
|
|
131 ENC_FRAME enc_frame;
|
|
132 ENC_RESULT enc_result;
|
|
133 void* enc_handle=NULL;
|
|
134
|
|
135 //int out_buffer_size=0x200000;
|
|
136 //unsigned char* out_buffer=malloc(out_buffer_size);
|
|
137
|
|
138 mp_msg_init(verbose+MSGL_STATUS);
|
|
139
|
|
140 // check codec.conf
|
|
141 if(!parse_codec_cfg("etc/codecs.conf")){
|
|
142 mp_msg(MSGT_CPLAYER,MSGL_HINT,MSGTR_CopyCodecsConf);
|
|
143 exit(0); // From unknown reason a hangup occurs here :((((((
|
|
144 }
|
|
145
|
|
146 if(argc>1)
|
|
147 stream=open_stream(argv[1],0,&file_format);
|
|
148 else
|
|
149 stream=open_stream("/3d/divx/405divx_sm_v2[1].avi",0,&file_format);
|
|
150 // stream=open_stream("/dev/cdrom",2,&file_format); // VCD track 2
|
|
151
|
|
152 if(!stream){
|
|
153 printf("Cannot open file/device\n");
|
|
154 exit(1);
|
|
155 }
|
|
156
|
|
157 printf("success: format: %d data: 0x%X - 0x%X\n",file_format, (int)(stream->start_pos),(int)(stream->end_pos));
|
|
158
|
|
159 stream_enable_cache(stream,2048*1024);
|
|
160
|
|
161 demuxer=demux_open(stream,file_format,-1,-1,-1);
|
|
162 if(!demuxer){
|
|
163 printf("Cannot open demuxer\n");
|
|
164 exit(1);
|
|
165 }
|
|
166
|
|
167 d_audio=demuxer->audio;
|
|
168 d_video=demuxer->video;
|
|
169 d_dvdsub=demuxer->sub;
|
|
170 sh_audio=d_audio->sh;
|
|
171 sh_video=d_video->sh;
|
|
172
|
|
173 if(!video_read_properties(sh_video)){
|
|
174 printf("Couldn't read video properties\n");
|
|
175 exit(1);
|
|
176 }
|
|
177
|
|
178 mp_msg(MSGT_CPLAYER,MSGL_INFO,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
|
|
179 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
|
|
180 sh_video->fps,sh_video->frametime
|
|
181 );
|
|
182
|
|
183
|
|
184 sh_video->codec=NULL;
|
|
185 if(video_family!=-1) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_TryForceVideoFmt,video_family);
|
|
186 while(1){
|
|
187 sh_video->codec=find_codec(sh_video->format,
|
|
188 sh_video->bih?((unsigned int*) &sh_video->bih->biCompression):NULL,sh_video->codec,0);
|
|
189 if(!sh_video->codec){
|
|
190 if(video_family!=-1) {
|
|
191 sh_video->codec=NULL; /* re-search */
|
|
192 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_CantFindVfmtFallback);
|
|
193 video_family=-1;
|
|
194 continue;
|
|
195 }
|
|
196 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantFindVideoCodec,sh_video->format);
|
|
197 mp_msg(MSGT_CPLAYER,MSGL_HINT, MSGTR_TryUpgradeCodecsConfOrRTFM,get_path("codecs.conf"));
|
|
198 exit(1);
|
|
199 }
|
|
200 if(video_codec && strcmp(sh_video->codec->name,video_codec)) continue;
|
|
201 else if(video_family!=-1 && sh_video->codec->driver!=video_family) continue;
|
|
202 break;
|
|
203 }
|
|
204
|
|
205 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s video codec: [%s] drv:%d (%s)\n",video_codec?"Forcing":"Detected",sh_video->codec->name,sh_video->codec->driver,sh_video->codec->info);
|
|
206
|
|
207 for(i=0;i<CODECS_MAX_OUTFMT;i++){
|
|
208 out_fmt=sh_video->codec->outfmt[i];
|
|
209 if(out_fmt==0xFFFFFFFF) continue;
|
|
210 if(out_fmt==IMGFMT_YV12) break;
|
|
211 if(out_fmt==IMGFMT_I420) break;
|
|
212 if(out_fmt==IMGFMT_IYUV) break;
|
|
213 if(out_fmt==IMGFMT_YUY2) break;
|
|
214 }
|
|
215 if(i>=CODECS_MAX_OUTFMT){
|
|
216 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_VOincompCodec);
|
|
217 exit(1); // exit_player(MSGTR_Exit_error);
|
|
218 }
|
|
219 sh_video->outfmtidx=i;
|
|
220
|
|
221 if(out_fmt==IMGFMT_YV12 || out_fmt==IMGFMT_I420 || out_fmt==IMGFMT_IYUV){
|
|
222 vo_w=sh_video->disp_w;
|
|
223 vo_h=sh_video->disp_h;
|
|
224 vo_image=malloc(vo_w*vo_h*3/2);
|
|
225 vo_image_ptr=vo_image;
|
|
226 }
|
|
227
|
|
228 if(!init_video(sh_video)){
|
|
229 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CouldntInitVideoCodec);
|
|
230 exit(1);
|
|
231 }
|
|
232
|
|
233 // set up video encoder:
|
|
234 video_out.draw_slice=draw_slice;
|
|
235 video_out.draw_frame=draw_frame;
|
|
236
|
|
237 // set up output file:
|
|
238 muxer_f=fopen("test.avi","wb");
|
|
239 muxer=aviwrite_new_muxer();
|
|
240 mux_v=aviwrite_new_stream(muxer,AVIWRITE_TYPE_VIDEO);
|
|
241
|
|
242 mux_v->buffer_size=0x200000;
|
|
243 mux_v->buffer=malloc(mux_v->buffer_size);
|
|
244
|
|
245 mux_v->source=sh_video;
|
|
246
|
|
247 mux_v->h.dwSampleSize=0; // VBR
|
|
248 mux_v->h.dwScale=10000;
|
|
249 mux_v->h.dwRate=mux_v->h.dwScale*sh_video->fps;
|
|
250
|
2574
|
251 mux_v->codec=VCODEC_DIVX4; // 0=streamcopy
|
|
252
|
2531
|
253 switch(mux_v->codec){
|
|
254 case 0:
|
|
255 mux_v->bih=sh_video->bih;
|
|
256 break;
|
|
257 case VCODEC_DIVX4:
|
|
258 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER));
|
|
259 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
|
|
260 mux_v->bih->biWidth=sh_video->disp_w;
|
|
261 mux_v->bih->biHeight=sh_video->disp_h;
|
|
262 mux_v->bih->biPlanes=0;
|
|
263 mux_v->bih->biBitCount=24;
|
|
264 mux_v->bih->biCompression=mmioFOURCC('d','i','v','x');
|
|
265 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
|
|
266 break;
|
|
267 }
|
|
268
|
|
269 aviwrite_write_header(muxer,muxer_f);
|
|
270
|
|
271 switch(mux_v->codec){
|
|
272 case 0:
|
|
273 break;
|
|
274 case VCODEC_DIVX4:
|
|
275 // init divx4linux:
|
|
276 enc_param.x_dim=sh_video->disp_w;
|
|
277 enc_param.y_dim=sh_video->disp_h;
|
|
278 enc_param.framerate=sh_video->fps;
|
|
279 enc_param.bitrate=800;
|
|
280 enc_param.rc_period=0;
|
|
281 enc_param.rc_reaction_period=0;
|
|
282 enc_param.rc_reaction_ratio=0;
|
|
283 enc_param.max_quantizer=0;
|
|
284 enc_param.min_quantizer=0;
|
|
285 enc_param.max_key_interval=0;
|
|
286 enc_param.use_bidirect=0;
|
|
287 enc_param.deinterlace=0;
|
|
288 enc_param.quality=5; // the quality of compression ( 1 - fastest, 5 - best )
|
|
289 enc_param.obmc=0;
|
|
290 enc_param.handle=NULL;
|
|
291 encore(NULL,ENC_OPT_INIT,&enc_param,NULL);
|
|
292 enc_handle=enc_param.handle;
|
|
293 break;
|
|
294 }
|
|
295
|
|
296
|
|
297 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
|
|
298 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
|
|
299 signal(SIGTERM,exit_sighandler); // kill
|
|
300
|
|
301
|
|
302 while(!eof){
|
|
303
|
2571
|
304 float frame_time=0;
|
2531
|
305 int blit_frame=0;
|
|
306 float a_pts=0;
|
|
307 float v_pts=0;
|
2574
|
308 unsigned char* start=NULL;
|
|
309 int in_size;
|
2531
|
310
|
2571
|
311 // get it!
|
|
312 // current_module="video_read_frame";
|
|
313 in_size=video_read_frame(sh_video,&frame_time,&start,force_fps);
|
|
314 if(in_size<0){ eof=1; break; }
|
2574
|
315 // if(in_size>max_framesize) max_framesize=in_size; // stats
|
|
316
|
2531
|
317 sh_video->timer+=frame_time;
|
|
318
|
|
319 if(pts_from_bps){
|
|
320 unsigned int samples=(sh_audio->audio.dwSampleSize)?
|
|
321 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
|
|
322 (d_audio->pack_no); // <- used for VBR audio
|
|
323 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
|
|
324 delay_corrected=1;
|
|
325 } else {
|
|
326 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
|
|
327 a_pts=d_audio->pts;
|
|
328 if(!delay_corrected) if(a_pts) delay_corrected=1;
|
|
329 //printf("*** %5.3f ***\n",a_pts);
|
|
330 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
|
|
331 }
|
|
332 v_pts=d_video->pts;
|
|
333
|
|
334 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"A:%6.1f V:%6.1f A-V:%7.3f ct:%7.3f %3d/%3d %2d%% %2d%% %4.1f%% %d%%\r",
|
|
335 a_pts,v_pts,a_pts-v_pts,c_total,
|
|
336 (int)sh_video->num_frames,(int)sh_video->num_frames_decoded,
|
|
337 (sh_video->timer>0.5)?(int)(100.0*video_time_usage/(double)sh_video->timer):0,
|
|
338 (sh_video->timer>0.5)?(int)(100.0*vout_time_usage/(double)sh_video->timer):0,
|
|
339 (sh_video->timer>0.5)?(100.0*audio_time_usage/(double)sh_video->timer):0
|
|
340 ,cache_fill_status
|
|
341 );
|
|
342 fflush(stdout);
|
|
343
|
2574
|
344 // if(!blit_frame) continue;
|
2531
|
345
|
|
346 switch(mux_v->codec){
|
2574
|
347 case 0:
|
|
348 mux_v->buffer=start;
|
|
349 aviwrite_write_chunk(muxer,mux_v,muxer_f,in_size,(sh_video->ds->flags&1)?0x10:0);
|
|
350 break;
|
2531
|
351 case VCODEC_DIVX4:
|
2574
|
352 blit_frame=decode_video(&video_out,sh_video,start,in_size,0);
|
|
353 if(!blit_frame){
|
|
354 // empty.
|
|
355 aviwrite_write_chunk(muxer,mux_v,muxer_f,0,0);
|
|
356 break;
|
|
357 }
|
2531
|
358 enc_frame.image=vo_image_ptr;
|
|
359 enc_frame.bitstream=mux_v->buffer;
|
|
360 enc_frame.length=mux_v->buffer_size;
|
|
361 switch(out_fmt){
|
|
362 case IMGFMT_YV12: enc_frame.colorspace=ENC_CSP_YV12; break;
|
|
363 case IMGFMT_IYUV:
|
|
364 case IMGFMT_I420: enc_frame.colorspace=ENC_CSP_I420; break;
|
|
365 case IMGFMT_YUY2: enc_frame.colorspace=ENC_CSP_YUY2; break;
|
|
366 case IMGFMT_UYVY: enc_frame.colorspace=ENC_CSP_UYVY; break;
|
|
367 case IMGFMT_RGB24:
|
|
368 case IMGFMT_BGR24:
|
|
369 enc_frame.colorspace=ENC_CSP_RGB24; break;
|
|
370 }
|
|
371 enc_frame.quant=0;
|
|
372 enc_frame.intra=0;
|
|
373 enc_frame.mvs=NULL;
|
|
374 // printf("encoding...\n");
|
|
375 encore(enc_handle,ENC_OPT_ENCODE,&enc_frame,&enc_result);
|
|
376 // printf(" len=%d key:%d qualt:%d \n",enc_frame.length,enc_result.is_key_frame,enc_result.quantizer);
|
|
377 aviwrite_write_chunk(muxer,mux_v,muxer_f,enc_frame.length,enc_result.is_key_frame?0x10:0);
|
|
378 break;
|
|
379 }
|
|
380
|
|
381
|
|
382 } // while(!eof)
|
|
383
|
|
384 aviwrite_write_index(muxer,muxer_f);
|
|
385 fseek(muxer_f,0,SEEK_SET);
|
|
386 aviwrite_write_header(muxer,muxer_f); // update header
|
|
387 fclose(muxer_f);
|
|
388
|
|
389 }
|