Details | 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 "sound.h" |
||
12 | #include "fplay.h" |
||
13 | |||
14 | |||
15 | |||
16 | |||
17 | |||
18 | |||
19 | |||
20 | |||
21 | AVCodecContext *pCodecCtx; |
||
22 | AVCodecContext *aCodecCtx; |
||
23 | AVCodec *pCodec; |
||
24 | AVCodec *aCodec; |
||
25 | AVFrame *pFrame; |
||
26 | int videoStream; |
||
27 | int audioStream; |
||
28 | |||
29 | |||
30 | |||
31 | |||
32 | |||
33 | extern int sample_rate; |
||
34 | |||
35 | |||
36 | { |
||
37 | int i; |
||
38 | |||
39 | |||
40 | printf("Please provide a movie file\n"); |
||
41 | return -1; |
||
42 | } |
||
43 | |||
44 | |||
45 | |||
46 | |||
47 | avdevice_register_all(); |
||
48 | av_register_all(); |
||
49 | |||
50 | |||
51 | |||
52 | if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0) |
||
53 | { |
||
54 | printf("Cannot open file %s\n\r", argv[1]); |
||
55 | return -1; // Couldn't open file |
||
56 | }; |
||
57 | |||
58 | |||
59 | |||
60 | |||
61 | if(av_find_stream_info(pFormatCtx)<0) |
||
62 | { |
||
63 | printf("Cannot find streams\n\r"); |
||
64 | return -1; |
||
65 | }; |
||
66 | |||
67 | |||
68 | |||
69 | |||
70 | |||
71 | |||
72 | videoStream=-1; |
||
73 | audioStream=-1; |
||
74 | for(i=0; i < pFormatCtx->nb_streams; i++) |
||
75 | { |
||
76 | if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO |
||
77 | && videoStream < 0) |
||
78 | { |
||
79 | videoStream=i; |
||
80 | video_time_base = pFormatCtx->streams[i]->time_base; |
||
81 | |||
82 | |||
83 | if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO && |
||
84 | audioStream < 0) |
||
85 | { |
||
86 | audioStream=i; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | |||
91 | { |
||
92 | printf("Video stream not detected\n\r"); |
||
93 | return -1; // Didn't find a video stream |
||
94 | } |
||
95 | |||
96 | |||
97 | |||
98 | pCodecCtx=pFormatCtx->streams[videoStream]->codec; |
||
99 | |||
100 | |||
101 | |||
102 | |||
103 | pCodec=avcodec_find_decoder(pCodecCtx->codec_id); |
||
104 | if(pCodec==NULL) { |
||
105 | printf("Unsupported video codec!\n"); |
||
106 | return -1; // Codec not found |
||
107 | } |
||
108 | // Open codec |
||
109 | if(avcodec_open(pCodecCtx, pCodec) < 0) |
||
110 | { |
||
111 | printf("Cannot open video codec\n\r"); |
||
112 | return -1; // Could not open codec |
||
113 | }; |
||
114 | |||
115 | |||
116 | aCodecCtx->request_channels = FFMIN(2, aCodecCtx->channels); |
||
117 | else |
||
118 | aCodecCtx->request_channels = 2; |
||
119 | |||
120 | |||
121 | |||
122 | |||
123 | { |
||
124 | if(avcodec_open(aCodecCtx, aCodec) >= 0 ) |
||
125 | { |
||
126 | WAVEHEADER whdr; |
||
127 | int fmt; |
||
128 | |||
129 | |||
130 | aCodecCtx->sample_rate, aCodecCtx->channels); |
||
131 | |||
132 | |||
133 | whdr.riff_format = 0x45564157; |
||
134 | whdr.wFormatTag = 0x01; |
||
135 | whdr.nSamplesPerSec = aCodecCtx->sample_rate; |
||
136 | whdr.nChannels = aCodecCtx->channels; |
||
137 | whdr.wBitsPerSample = 16; |
||
138 | |||
139 | |||
140 | |||
141 | |||
142 | |||
143 | |||
144 | { |
||
145 | decoder_buffer = (uint8_t*)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE); |
||
146 | if( decoder_buffer != NULL ) |
||
147 | { |
||
148 | astream.lock = 0; |
||
149 | astream.count = 0; |
||
150 | astream.buffer = (char *)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE*8); |
||
151 | if( astream.buffer != NULL ) |
||
152 | have_sound = 1; |
||
153 | else |
||
154 | av_free(decoder_buffer); |
||
155 | } |
||
156 | if( have_sound == 0) |
||
157 | { |
||
158 | printf("Not enough memory for audio buffers\n"); |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | else printf("Cannot open audio codec\n\r"); |
||
163 | } |
||
164 | else printf("Unsupported audio codec!\n"); |
||
165 | |||
166 | |||
167 | return 0; |
||
168 | |||
169 | |||
170 | // Note that pFrameRGB is an AVFrame, but AVFrame is a superset |
||
171 | // of AVPicture |
||
172 | |||
173 | |||
174 | |||
175 | |||
176 | |||
177 | |||
178 | |||
179 | |||
180 | |||
181 | av_free(pFrame); |
||
182 | |||
183 | |||
184 | |||
185 | |||
186 | // avcodec_close(pCodecCtx); |
||
187 | |||
188 | |||
189 | // av_close_input_file(pFormatCtx); |
||
190 | |||
191 | |||
192 | |||
193 | |||
194 | } |
||
195 | |||
196 | |||
197 | { |
||
198 | AVPacket packet; |
||
199 | |||
200 | |||
201 | { |
||
202 | if(packet.stream_index==videoStream) |
||
203 | { |
||
204 | decode_video(pCodecCtx, &packet); |
||
205 | } |
||
206 | else if( (packet.stream_index == audioStream) && |
||
207 | (have_sound != 0) ) |
||
208 | { |
||
209 | uint8_t *audio_data; |
||
210 | int audio_size; |
||
211 | int len; |
||
212 | int data_size=0; |
||
213 | |||
214 | |||
215 | audio_size = packet.size; |
||
216 | |||
217 | |||
218 | { |
||
219 | data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; |
||
220 | |||
221 | |||
222 | &data_size, audio_data, audio_size); |
||
223 | |||
224 | |||
225 | { |
||
226 | audio_data += len; |
||
227 | audio_size -= len; |
||
228 | |||
229 | |||
230 | AVCODEC_MAX_AUDIO_FRAME_SIZE*8) |
||
231 | { |
||
232 | yield(); |
||
233 | } |
||
234 | spinlock_lock(&astream.lock); |
||
235 | memcpy(astream.buffer+astream.count, decoder_buffer, data_size); |
||
236 | astream.count += data_size; |
||
237 | spinlock_unlock(&astream.lock); |
||
238 | } |
||
239 | else audio_size = 0; |
||
240 | } |
||
241 | } |
||
242 | // Free the packet that was allocated by av_read_frame |
||
243 | av_free_packet(&packet); |
||
244 | }; |
||
245 | }; |
||
246 | |||
247 | |||
248 | |||
249 | { |
||
250 | int off = offset; |
||
251 | return lseek(fd, off, origin); |
||
252 | }>>>>0) |
||
253 | >> |
||
254 |