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