Rev 2349 | 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 |
2415 | Serge | 13 | |
2349 | Serge | 14 | |
1696 | serge | 15 | #include "fplay.h" |
16 | |||
17 | |||
18 | |||
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 | |||
2349 | Serge | 105 | |
1696 | serge | 106 | |
107 | pCodecCtx=pFormatCtx->streams[videoStream]->codec; |
||
108 | aCodecCtx=pFormatCtx->streams[audioStream]->codec; |
||
109 | |||
110 | |||
111 | pCodec=avcodec_find_decoder(pCodecCtx->codec_id); |
||
112 | if(pCodec==NULL) { |
||
113 | printf("Unsupported codec with id %d for input stream %d\n", |
||
2349 | Serge | 114 | pCodecCtx->codec_id, videoStream); |
115 | return -1; // Codec not found |
||
1696 | serge | 116 | } |
117 | |||
2349 | Serge | 118 | |
119 | { |
||
1696 | serge | 120 | printf("Error while opening codec for input stream %d\n", |
2349 | Serge | 121 | videoStream); |
122 | return -1; // Could not open codec |
||
1696 | serge | 123 | }; |
124 | |||
125 | |||
126 | aCodecCtx->request_channels = FFMIN(2, aCodecCtx->channels); |
||
127 | else |
||
128 | aCodecCtx->request_channels = 2; |
||
129 | |||
130 | |||
131 | |||
132 | |||
133 | { |
||
134 | if(avcodec_open2(aCodecCtx, aCodec, NULL) >= 0 ) |
||
2349 | Serge | 135 | { |
1696 | serge | 136 | WAVEHEADER whdr; |
137 | int fmt; |
||
138 | |||
139 | |||
140 | aCodecCtx->sample_rate, aCodecCtx->channels); |
||
141 | |||
142 | |||
143 | whdr.riff_format = 0x45564157; |
||
144 | whdr.wFormatTag = 0x01; |
||
145 | whdr.nSamplesPerSec = aCodecCtx->sample_rate; |
||
146 | whdr.nChannels = aCodecCtx->channels; |
||
147 | whdr.wBitsPerSample = 16; |
||
148 | |||
149 | |||
150 | |||
151 | |||
152 | |||
153 | |||
154 | { |
||
155 | decoder_buffer = (uint8_t*)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE*2+64); |
||
2349 | Serge | 156 | if( decoder_buffer != NULL ) |
1696 | serge | 157 | { |
158 | astream.lock = 0; |
||
159 | astream.count = 0; |
||
160 | astream.buffer = (char *)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE*8); |
||
161 | if( astream.buffer != NULL ) |
||
162 | have_sound = 1; |
||
163 | else |
||
164 | av_free(decoder_buffer); |
||
165 | } |
||
166 | if( have_sound == 0) |
||
167 | { |
||
168 | printf("Not enough memory for audio buffers\n"); |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | else printf("Cannot open audio codec\n\r"); |
||
173 | } |
||
174 | else printf("Unsupported audio codec!\n"); |
||
175 | |||
176 | |||
177 | return 0; |
||
178 | |||
179 | |||
2349 | Serge | 180 | |
1696 | serge | 181 | |
182 | |||
183 | |||
184 | av_free(pFrame); |
||
185 | |||
186 | |||
2349 | Serge | 187 | |
1696 | serge | 188 | |
189 | |||
190 | // avcodec_close(pCodecCtx); |
||
191 | |||
192 | |||
193 | // av_close_input_file(pFormatCtx); |
||
194 | |||
195 | |||
196 | |||
197 | |||
198 | } |
||
199 | |||
200 | |||
201 | { |
||
202 | int eof = 0; |
||
2349 | Serge | 203 | AVPacket packet; |
204 | int ret; |
||
205 | |||
1696 | serge | 206 | |
2349 | Serge | 207 | { |
1696 | serge | 208 | int err; |
2349 | Serge | 209 | |
1696 | serge | 210 | |
2349 | Serge | 211 | |
1696 | serge | 212 | |
2415 | Serge | 213 | { |
2349 | Serge | 214 | err = av_read_frame(pFormatCtx, &packet); |
215 | if( err < 0) |
||
216 | { |
||
1696 | serge | 217 | eof = 1; |
2349 | Serge | 218 | if (err != AVERROR_EOF) |
219 | printf("av_read_frame: error %x\n", err); |
||
220 | continue; |
||
221 | } |
||
222 | if(packet.stream_index==videoStream) |
||
223 | { |
||
224 | put_packet(&q_video, &packet); |
||
225 | } |
||
226 | else if( (packet.stream_index == audioStream) && |
||
227 | (have_sound != 0) ) |
||
228 | { |
||
229 | put_packet(&q_audio, &packet); |
||
230 | } |
||
231 | else |
||
232 | { |
||
233 | av_free_packet(&packet); |
||
234 | }; |
||
235 | decode_video(pCodecCtx, &q_video); |
||
236 | decode_audio(aCodecCtx, &q_audio); |
||
237 | continue; |
||
238 | }; |
||
239 | decode_video(pCodecCtx, &q_video); |
||
240 | decode_audio(aCodecCtx, &q_audio); |
||
241 | delay(1); |
||
242 | }; |
||
243 | |||
1696 | serge | 244 | |
2349 | Serge | 245 | |
1696 | serge | 246 | |
2415 | Serge | 247 | { |
2349 | Serge | 248 | ret = decode_video(pCodecCtx, &q_video); |
249 | ret |= decode_audio(aCodecCtx, &q_audio); |
||
250 | delay(1); |
||
251 | }; |
||
1696 | serge | 252 | delay(50); |
2349 | Serge | 253 | status = 0; |
254 | printf("status = 0\n"); |
||
255 | delay(300); |
||
2415 | Serge | 256 | };>>>>>>0) |
1696 | serge | 257 | >>> |
258 |