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