Rev 4438 | Rev 5592 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4438 | Serge | 1 | |
2 | |||
3 | |||
4 | #include |
||
5 | #include |
||
6 | #include |
||
7 | |||
8 | |||
9 | #include |
||
10 | #include |
||
11 | #include |
||
12 | #include |
||
13 | #include "winlib/winlib.h" |
||
5021 | Serge | 14 | |
4438 | Serge | 15 | |
16 | #include "fplay.h" |
||
17 | |||
18 | |||
19 | volatile enum player_state decoder_state = PREPARE; |
||
20 | volatile enum player_state sound_state = STOP; |
||
21 | |||
22 | |||
23 | |||
24 | |||
25 | |||
26 | |||
27 | AVCodecContext *pCodecCtx; |
||
28 | AVCodecContext *aCodecCtx; |
||
29 | AVCodec *pCodec; |
||
30 | AVCodec *aCodec; |
||
31 | AVFrame *pFrame; |
||
32 | int videoStream; |
||
33 | int audioStream; |
||
34 | |||
35 | |||
36 | |||
37 | |||
38 | extern int resampler_size; |
||
39 | |||
40 | |||
41 | char *movie_file; |
||
42 | |||
43 | |||
44 | |||
45 | |||
46 | queue_t q_audio; |
||
47 | int64_t rewind_pos; |
||
48 | |||
49 | |||
50 | |||
51 | |||
52 | |||
53 | |||
54 | { |
||
55 | |||
56 | |||
57 | |||
58 | |||
59 | |||
60 | |||
61 | |||
62 | |||
63 | { |
||
64 | int i; |
||
65 | char *file_name, *dot; |
||
66 | |||
67 | |||
68 | { |
||
69 | movie_file = get_moviefile(); |
||
70 | if(movie_file == NULL) |
||
71 | { |
||
72 | printf("Please provide a movie file\n"); |
||
73 | return -1; |
||
74 | } |
||
75 | } |
||
76 | else movie_file = argv[1]; |
||
77 | |||
78 | |||
79 | |||
80 | |||
81 | { |
||
82 | int fd, i; |
||
83 | char *buff; |
||
84 | uint32_t start, stop; |
||
85 | |||
86 | |||
87 | |||
88 | |||
89 | return 0; |
||
90 | |||
91 | |||
92 | memset(buff, 0, 65536); |
||
93 | |||
94 | |||
95 | |||
96 | |||
97 | { |
||
98 | if(read(fd, buff, 65536) < 0) |
||
99 | break; |
||
100 | |||
101 | |||
102 | stop = get_tick_count(); |
||
103 | |||
104 | |||
105 | }; |
||
106 | return 0; |
||
107 | }; |
||
108 | |||
109 | |||
110 | |||
111 | |||
112 | |||
113 | |||
114 | avdevice_register_all(); |
||
115 | av_register_all(); |
||
116 | |||
117 | |||
118 | |||
119 | |||
120 | { |
||
121 | printf("Cannot open file %s\n\r", movie_file); |
||
122 | return -1; // Couldn't open file |
||
123 | }; |
||
124 | |||
125 | |||
126 | |||
127 | |||
128 | if(avformat_find_stream_info(pFormatCtx, NULL)<0) |
||
129 | { |
||
130 | printf("Cannot find streams\n\r"); |
||
131 | return -1; |
||
132 | }; |
||
133 | |||
134 | |||
135 | dot = strrchr(file_name,'.'); |
||
136 | if(dot) |
||
137 | { |
||
138 | movie_file = malloc(dot-file_name+1); |
||
139 | memcpy(movie_file, file_name, dot-file_name); |
||
140 | movie_file[dot-file_name] = 0; |
||
141 | } |
||
142 | else movie_file = file_name; |
||
143 | |||
144 | |||
145 | |||
146 | |||
147 | |||
148 | |||
149 | // Find the first video stream |
||
150 | videoStream=-1; |
||
151 | audioStream=-1; |
||
152 | for(i=0; i < pFormatCtx->nb_streams; i++) |
||
153 | { |
||
154 | if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO |
||
155 | && videoStream < 0) |
||
156 | { |
||
157 | videoStream=i; |
||
158 | video_time_base = pFormatCtx->streams[i]->time_base; |
||
159 | if(stream_duration == 0) |
||
160 | stream_duration = pFormatCtx->streams[i]->duration; |
||
161 | |||
162 | |||
163 | if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO && |
||
164 | audioStream < 0) |
||
165 | { |
||
166 | audioStream=i; |
||
167 | if(stream_duration == 0) |
||
168 | stream_duration = pFormatCtx->streams[i]->duration; |
||
169 | } |
||
170 | } |
||
171 | |||
172 | |||
173 | { |
||
174 | printf("Video stream not detected\n\r"); |
||
175 | return -1; // Didn't find a video stream |
||
176 | }; |
||
177 | |||
178 | |||
179 | { |
||
180 | AVPacket packet; |
||
181 | int psize = 0; |
||
182 | uint32_t start, stop; |
||
183 | |||
184 | |||
185 | start = get_tick_count(); |
||
186 | |||
187 | |||
188 | { |
||
189 | err = av_read_frame(pFormatCtx, &packet); |
||
190 | if(err != 0) |
||
191 | break; |
||
192 | psize+= packet.size; |
||
193 | av_free_packet(&packet); |
||
194 | }; |
||
195 | |||
196 | |||
197 | |||
198 | |||
199 | |||
200 | |||
201 | }; |
||
202 | }; |
||
203 | #else |
||
204 | |||
205 | |||
206 | |||
207 | |||
208 | |||
209 | pCodecCtx=pFormatCtx->streams[videoStream]->codec; |
||
210 | aCodecCtx=pFormatCtx->streams[audioStream]->codec; |
||
211 | |||
212 | |||
213 | |||
214 | |||
215 | |||
216 | |||
217 | |||
218 | |||
219 | |||
220 | |||
221 | printf("Unsupported codec with id %d for input stream %d\n", |
||
222 | pCodecCtx->codec_id, videoStream); |
||
223 | return -1; // Codec not found |
||
224 | } |
||
225 | |||
226 | |||
227 | { |
||
228 | printf("Error while opening codec for input stream %d\n", |
||
229 | videoStream); |
||
230 | return -1; // Could not open codec |
||
231 | }; |
||
232 | |||
233 | |||
234 | |||
235 | |||
236 | |||
237 | aCodecCtx->request_channels = FFMIN(2, aCodecCtx->channels); |
||
238 | else |
||
239 | aCodecCtx->request_channels = 2; |
||
240 | |||
241 | |||
242 | |||
243 | |||
244 | { |
||
245 | if(avcodec_open2(aCodecCtx, aCodec, NULL) >= 0 ) |
||
246 | { |
||
247 | WAVEHEADER whdr; |
||
248 | int fmt; |
||
249 | int channels; |
||
250 | |||
251 | |||
252 | aCodecCtx->sample_rate, aCodecCtx->channels, aCodecCtx->sample_fmt ); |
||
253 | whdr.riff_id = 0x46464952; |
||
254 | whdr.riff_format = 0x45564157; |
||
255 | whdr.wFormatTag = 0x01; |
||
256 | whdr.nSamplesPerSec = aCodecCtx->sample_rate; |
||
257 | whdr.nChannels = 2; |
||
258 | whdr.wBitsPerSample = 16; |
||
259 | |||
260 | |||
261 | |||
262 | |||
263 | |||
264 | |||
265 | { |
||
266 | decoder_buffer = (uint8_t*)av_mallocz(192000*2+64); |
||
267 | if( decoder_buffer != NULL ) |
||
268 | { |
||
269 | astream.lock = 0; |
||
270 | astream.count = 0; |
||
271 | astream.buffer = (char *)av_mallocz(192000*3); |
||
272 | if( astream.buffer != NULL ) |
||
273 | have_sound = 1; |
||
274 | else |
||
275 | av_free(decoder_buffer); |
||
276 | } |
||
277 | if( have_sound == 0) |
||
278 | { |
||
279 | printf("Not enough memory for audio buffers\n"); |
||
280 | } |
||
281 | } |
||
282 | } |
||
283 | else printf("Cannot open audio codec\n\r"); |
||
284 | } |
||
285 | else printf("Unsupported audio codec!\n"); |
||
286 | |||
287 | |||
288 | return 0; |
||
289 | |||
290 | |||
291 | |||
292 | |||
293 | |||
294 | |||
295 | av_free(pFrame); |
||
296 | |||
297 | |||
298 | |||
299 | |||
300 | |||
301 | // avcodec_close(pCodecCtx); |
||
302 | |||
303 | |||
304 | // av_close_input_file(pFormatCtx); |
||
305 | |||
306 | |||
307 | |||
308 | |||
309 | } |
||
310 | |||
311 | |||
312 | |||
313 | { |
||
314 | AVPacket packet; |
||
315 | int err; |
||
316 | |||
317 | |||
318 | if( err == 0) |
||
319 | { |
||
320 | if(packet.stream_index==videoStream) |
||
321 | put_packet(&q_video, &packet); |
||
322 | else if( (packet.stream_index == audioStream) && |
||
323 | (have_sound != 0) ) |
||
324 | { |
||
325 | put_packet(&q_audio, &packet); |
||
326 | if(audio_base == -1.0) |
||
327 | { |
||
328 | if (packet.pts != AV_NOPTS_VALUE) |
||
329 | audio_base = get_audio_base() * packet.pts; |
||
330 | // printf("audio base %f\n", audio_base); |
||
331 | }; |
||
332 | } |
||
333 | else av_free_packet(&packet); |
||
334 | } |
||
335 | else if (err != AVERROR_EOF) |
||
336 | printf("av_read_frame: error %x\n", err); |
||
337 | |||
338 | |||
339 | } |
||
340 | |||
341 | |||
342 | |||
343 | |||
344 | { |
||
345 | int err = 0; |
||
346 | AVPacket packet; |
||
347 | |||
348 | |||
349 | !err ) |
||
350 | err = load_frame(); |
||
351 | |||
352 | |||
353 | |||
354 | |||
355 | |||
356 | |||
357 | |||
358 | { |
||
359 | AVPacket packet; |
||
360 | |||
361 | |||
362 | avcodec_flush_buffers(aCodecCtx); |
||
363 | while( get_packet(&q_video, &packet) != 0) |
||
364 | av_free_packet(&packet); |
||
365 | |||
366 | |||
367 | av_free_packet(&packet); |
||
368 | |||
369 | |||
370 | |||
371 | |||
372 | }; |
||
373 | |||
374 | |||
375 | { |
||
376 | int eof; |
||
377 | AVPacket packet; |
||
378 | int ret, vret, aret; |
||
379 | |||
380 | |||
381 | |||
382 | |||
383 | { |
||
384 | int err; |
||
385 | |||
386 | |||
387 | { |
||
388 | case PREPARE: |
||
389 | eof = fill_queue(); |
||
390 | |||
391 | |||
392 | { |
||
393 | if( (q_video.size < 4*1024*1024) && |
||
394 | (eof == 0) ) |
||
395 | { |
||
396 | eof = load_frame(); |
||
397 | } |
||
398 | decode_video(pCodecCtx, &q_video); |
||
399 | ret = decode_audio(aCodecCtx, &q_audio); |
||
400 | }while(astream.count < resampler_size*2 && |
||
401 | ret == 1); |
||
402 | |||
403 | |||
404 | decoder_state = PLAY; |
||
405 | player_state = PLAY; |
||
406 | |||
407 | |||
408 | if( (q_video.size < 4*1024*1024) && |
||
409 | (eof == 0) ) |
||
410 | { |
||
411 | eof = load_frame(); |
||
412 | } |
||
413 | vret = decode_video(pCodecCtx, &q_video); |
||
414 | aret = decode_audio(aCodecCtx, &q_audio); |
||
415 | ret = vret | aret; |
||
416 | |||
417 | |||
418 | { |
||
419 | decoder_state = STOP; |
||
420 | continue; |
||
421 | }; |
||
422 | |||
423 | |||
424 | { |
||
425 | if( (q_video.size < 4*1024*1024) && |
||
426 | (eof == 0) ) |
||
427 | { |
||
428 | eof = load_frame(); |
||
429 | yield(); |
||
430 | continue; |
||
431 | }; |
||
432 | delay(1); |
||
433 | continue; |
||
434 | } |
||
435 | |||
436 | |||
437 | continue; |
||
438 | |||
439 | |||
440 | delay(1); |
||
441 | continue; |
||
442 | |||
443 | |||
444 | |||
445 | while(sound_state != STOP) |
||
446 | delay(1); |
||
447 | |||
448 | |||
449 | |||
450 | |||
451 | rewind_pos = pFormatCtx->start_time; |
||
452 | else |
||
453 | rewind_pos = 0; |
||
454 | |||
455 | |||
456 | rewind_pos, INT64_MAX, 0); |
||
457 | |||
458 | |||
459 | break; |
||
460 | |||
461 | |||
462 | while(sound_state != STOP) |
||
463 | yield(); |
||
464 | |||
465 | |||
466 | int opts = 0; |
||
467 | if(rewind_pos < 0) |
||
468 | { |
||
469 | rewind_pos = -rewind_pos; |
||
470 | opts = AVSEEK_FLAG_BACKWARD; |
||
471 | }; |
||
472 | |||
473 | |||
474 | rewind_pos += pFormatCtx->start_time; |
||
475 | |||
476 | |||
477 | min_pos = rewind_pos - 1000000; |
||
478 | max_pos = rewind_pos + 1000000; |
||
479 | |||
480 | |||
481 | rewind_pos, INT64_MAX, 0); |
||
482 | |||
483 | |||
484 | { |
||
485 | printf("could not seek to position %f\n", |
||
486 | (double)rewind_pos / AV_TIME_BASE); |
||
487 | } |
||
488 | |||
489 | |||
490 | decoder_state = PREPARE; |
||
491 | break; |
||
492 | } |
||
493 | }; |
||
494 | |||
495 | |||
496 | |||
497 | |||
498 | { |
||
499 | ret = decode_video(pCodecCtx, &q_video); |
||
500 | ret |= decode_audio(aCodecCtx, &q_audio); |
||
501 | delay(1); |
||
502 | }; |
||
503 | delay(50); |
||
504 | player_state = CLOSED; |
||
505 | delay(300); |
||
506 | }; |
||
507 | #endif |
||
508 | #endif>>>>>>>>>>>>0) |
||
509 | >>>>>> |