Rev 2415 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1696 | serge | 1 | |
2 | |||
3 | |||
4 | #include |
||
5 | #include |
||
6 | #include |
||
7 | |||
8 | |||
9 | #include |
||
10 | #include |
||
11 | #include |
||
2349 | Serge | 12 | #include "../winlib/winlib.h" |
2427 | Serge | 13 | |
2349 | Serge | 14 | |
1696 | serge | 15 | #include "fplay.h" |
16 | |||
17 | |||
2427 | Serge | 18 | |
1696 | serge | 19 | |
20 | |||
21 | |||
22 | |||
23 | |||
24 | AVCodecContext *pCodecCtx; |
||
25 | AVCodecContext *aCodecCtx; |
||
26 | AVCodec *pCodec; |
||
27 | AVCodec *aCodec; |
||
28 | AVFrame *pFrame; |
||
29 | int videoStream; |
||
30 | int audioStream; |
||
31 | |||
32 | |||
33 | |||
34 | |||
35 | extern int sample_rate; |
||
36 | char *movie_file; |
||
2349 | Serge | 37 | |
1696 | serge | 38 | |
2349 | Serge | 39 | |
40 | queue_t q_audio; |
||
41 | |||
42 | |||
1696 | serge | 43 | { |
44 | int i; |
||
45 | |||
46 | |||
47 | printf("Please provide a movie file\n"); |
||
48 | return -1; |
||
49 | } |
||
50 | |||
51 | |||
2349 | Serge | 52 | /* register all codecs, demux and protocols */ |
1696 | serge | 53 | |
54 | |||
2415 | Serge | 55 | |
56 | |||
1696 | serge | 57 | avdevice_register_all(); |
58 | av_register_all(); |
||
59 | |||
60 | |||
2349 | Serge | 61 | { |
1696 | serge | 62 | printf("Cannot open file %s\n\r", argv[1]); |
63 | return -1; // Couldn't open file |
||
64 | }; |
||
65 | |||
66 | |||
2415 | Serge | 67 | |
1696 | serge | 68 | |
69 | if(avformat_find_stream_info(pFormatCtx, NULL)<0) |
||
2349 | Serge | 70 | { |
1696 | serge | 71 | printf("Cannot find streams\n\r"); |
72 | return -1; |
||
73 | }; |
||
74 | |||
75 | |||
2349 | Serge | 76 | |
1696 | serge | 77 | |
2415 | Serge | 78 | |
1696 | serge | 79 | |
80 | videoStream=-1; |
||
81 | audioStream=-1; |
||
82 | for(i=0; i < pFormatCtx->nb_streams; i++) |
||
83 | { |
||
84 | if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO |
||
2349 | Serge | 85 | && videoStream < 0) |
1696 | serge | 86 | { |
87 | videoStream=i; |
||
88 | video_time_base = pFormatCtx->streams[i]->time_base; |
||
89 | |||
90 | |||
91 | if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO && |
||
2349 | Serge | 92 | audioStream < 0) |
1696 | serge | 93 | { |
94 | audioStream=i; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | |||
99 | { |
||
100 | printf("Video stream not detected\n\r"); |
||
101 | return -1; // Didn't find a video stream |
||
102 | } |
||
103 | |||
104 | |||
2427 | Serge | 105 | |
106 | |||
2349 | Serge | 107 | |
1696 | serge | 108 | |
109 | pCodecCtx=pFormatCtx->streams[videoStream]->codec; |
||
110 | aCodecCtx=pFormatCtx->streams[audioStream]->codec; |
||
111 | |||
112 | |||
113 | pCodec=avcodec_find_decoder(pCodecCtx->codec_id); |
||
114 | if(pCodec==NULL) { |
||
115 | printf("Unsupported codec with id %d for input stream %d\n", |
||
2349 | Serge | 116 | pCodecCtx->codec_id, videoStream); |
117 | return -1; // Codec not found |
||
1696 | serge | 118 | } |
119 | |||
2349 | Serge | 120 | |
121 | { |
||
1696 | serge | 122 | printf("Error while opening codec for input stream %d\n", |
2349 | Serge | 123 | videoStream); |
124 | return -1; // Could not open codec |
||
1696 | serge | 125 | }; |
126 | |||
127 | |||
128 | aCodecCtx->request_channels = FFMIN(2, aCodecCtx->channels); |
||
129 | else |
||
130 | aCodecCtx->request_channels = 2; |
||
131 | |||
132 | |||
133 | |||
134 | |||
135 | { |
||
136 | if(avcodec_open2(aCodecCtx, aCodec, NULL) >= 0 ) |
||
2349 | Serge | 137 | { |
1696 | serge | 138 | WAVEHEADER whdr; |
139 | int fmt; |
||
140 | |||
141 | |||
142 | aCodecCtx->sample_rate, aCodecCtx->channels); |
||
143 | |||
144 | |||
145 | whdr.riff_format = 0x45564157; |
||
146 | whdr.wFormatTag = 0x01; |
||
147 | whdr.nSamplesPerSec = aCodecCtx->sample_rate; |
||
148 | whdr.nChannels = aCodecCtx->channels; |
||
149 | whdr.wBitsPerSample = 16; |
||
150 | |||
151 | |||
152 | |||
153 | |||
154 | |||
155 | |||
156 | { |
||
157 | decoder_buffer = (uint8_t*)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE*2+64); |
||
2349 | Serge | 158 | if( decoder_buffer != NULL ) |
1696 | serge | 159 | { |
160 | astream.lock = 0; |
||
161 | astream.count = 0; |
||
162 | astream.buffer = (char *)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE*8); |
||
163 | if( astream.buffer != NULL ) |
||
164 | have_sound = 1; |
||
165 | else |
||
166 | av_free(decoder_buffer); |
||
167 | } |
||
168 | if( have_sound == 0) |
||
169 | { |
||
170 | printf("Not enough memory for audio buffers\n"); |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | else printf("Cannot open audio codec\n\r"); |
||
175 | } |
||
176 | else printf("Unsupported audio codec!\n"); |
||
177 | |||
178 | |||
179 | return 0; |
||
180 | |||
181 | |||
2349 | Serge | 182 | |
1696 | serge | 183 | |
184 | |||
185 | |||
186 | av_free(pFrame); |
||
187 | |||
188 | |||
2349 | Serge | 189 | |
1696 | serge | 190 | |
191 | |||
192 | // avcodec_close(pCodecCtx); |
||
193 | |||
194 | |||
195 | // av_close_input_file(pFormatCtx); |
||
196 | |||
197 | |||
198 | |||
199 | |||
200 | } |
||
201 | |||
202 | |||
203 | { |
||
204 | int eof = 0; |
||
2349 | Serge | 205 | AVPacket packet; |
206 | int ret; |
||
207 | |||
1696 | serge | 208 | |
2427 | Serge | 209 | { |
1696 | serge | 210 | int err; |
2349 | Serge | 211 | |
1696 | serge | 212 | |
2349 | Serge | 213 | |
1696 | serge | 214 | |
2427 | Serge | 215 | { |
216 | delay(1); |
||
217 | continue; |
||
218 | }; |
||
219 | |||
220 | |||
2415 | Serge | 221 | { |
2349 | Serge | 222 | err = av_read_frame(pFormatCtx, &packet); |
223 | if( err < 0) |
||
224 | { |
||
1696 | serge | 225 | eof = 1; |
2349 | Serge | 226 | if (err != AVERROR_EOF) |
227 | printf("av_read_frame: error %x\n", err); |
||
228 | continue; |
||
229 | } |
||
230 | if(packet.stream_index==videoStream) |
||
231 | { |
||
232 | put_packet(&q_video, &packet); |
||
233 | } |
||
234 | else if( (packet.stream_index == audioStream) && |
||
235 | (have_sound != 0) ) |
||
236 | { |
||
237 | put_packet(&q_audio, &packet); |
||
238 | } |
||
239 | else |
||
240 | { |
||
241 | av_free_packet(&packet); |
||
242 | }; |
||
243 | decode_video(pCodecCtx, &q_video); |
||
244 | decode_audio(aCodecCtx, &q_audio); |
||
245 | continue; |
||
246 | }; |
||
247 | decode_video(pCodecCtx, &q_video); |
||
248 | decode_audio(aCodecCtx, &q_audio); |
||
249 | delay(1); |
||
250 | }; |
||
251 | |||
1696 | serge | 252 | |
2349 | Serge | 253 | |
1696 | serge | 254 | |
2427 | Serge | 255 | { |
2349 | Serge | 256 | ret = decode_video(pCodecCtx, &q_video); |
257 | ret |= decode_audio(aCodecCtx, &q_audio); |
||
258 | delay(1); |
||
259 | }; |
||
1696 | serge | 260 | delay(50); |
2349 | Serge | 261 | player_state = CLOSED; |
2427 | Serge | 262 | delay(300); |
2415 | Serge | 263 | };>>>>>>0) |
1696 | serge | 264 | >>> |
265 |