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