... | ... |
@@ -178,6 +178,7 @@ External library support: |
178 | 178 |
--enable-librtmp enable RTMP[E] support via librtmp [no] |
179 | 179 |
--enable-libschroedinger enable Dirac support via libschroedinger [no] |
180 | 180 |
--enable-libspeex enable Speex decoding via libspeex [no] |
181 |
+ --enable-libstagefright enable H.264 decoding via libstagefright [no] |
|
181 | 182 |
--enable-libtheora enable Theora encoding via libtheora [no] |
182 | 183 |
--enable-libvo-aacenc enable AAC encoding via libvo-aacenc [no] |
183 | 184 |
--enable-libvo-amrwbenc enable AMR-WB encoding via libvo-amrwbenc [no] |
... | ... |
@@ -999,6 +1000,7 @@ CONFIG_LIST=" |
999 | 999 |
librtmp |
1000 | 1000 |
libschroedinger |
1001 | 1001 |
libspeex |
1002 |
+ libstagefright |
|
1002 | 1003 |
libtheora |
1003 | 1004 |
libvo_aacenc |
1004 | 1005 |
libvo_amrwbenc |
... | ... |
@@ -1463,6 +1465,7 @@ libopenjpeg_decoder_deps="libopenjpeg" |
1463 | 1463 |
libschroedinger_decoder_deps="libschroedinger" |
1464 | 1464 |
libschroedinger_encoder_deps="libschroedinger" |
1465 | 1465 |
libspeex_decoder_deps="libspeex" |
1466 |
+libstagefright_decoder_deps="libstagefright" |
|
1466 | 1467 |
libtheora_encoder_deps="libtheora" |
1467 | 1468 |
libvo_aacenc_encoder_deps="libvo_aacenc" |
1468 | 1469 |
libvo_amrwbenc_encoder_deps="libvo_amrwbenc" |
... | ... |
@@ -2946,6 +2949,9 @@ enabled libopenjpeg && require libopenjpeg openjpeg.h opj_version -lopenjpeg |
2946 | 2946 |
enabled librtmp && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket |
2947 | 2947 |
enabled libschroedinger && require_pkg_config schroedinger-1.0 schroedinger/schro.h schro_init |
2948 | 2948 |
enabled libspeex && require libspeex speex/speex.h speex_decoder_init -lspeex |
2949 |
+enabled libstagefright && require_cpp libstagefright "binder/ProcessState.h media/stagefright/MetaData.h |
|
2950 |
+ media/stagefright/MediaBufferGroup.h media/stagefright/MediaDebug.h media/stagefright/MediaDefs.h |
|
2951 |
+ media/stagefright/OMXClient.h media/stagefright/OMXCodec.h" android::OMXClient -lstagefright -lmedia -lutils -lbinder |
|
2949 | 2952 |
enabled libtheora && require libtheora theora/theoraenc.h th_info_init -ltheoraenc -ltheoradec -logg |
2950 | 2953 |
enabled libvo_aacenc && require libvo_aacenc vo-aacenc/voAAC.h voGetAACEncAPI -lvo-aacenc |
2951 | 2954 |
enabled libvo_amrwbenc && require libvo_amrwbenc vo-amrwbenc/enc_if.h E_IF_init -lvo-amrwbenc |
... | ... |
@@ -3226,6 +3232,7 @@ echo "libopenjpeg enabled ${libopenjpeg-no}" |
3226 | 3226 |
echo "librtmp enabled ${librtmp-no}" |
3227 | 3227 |
echo "libschroedinger enabled ${libschroedinger-no}" |
3228 | 3228 |
echo "libspeex enabled ${libspeex-no}" |
3229 |
+echo "libstagefright enabled ${libstagefright-no}" |
|
3229 | 3230 |
echo "libtheora enabled ${libtheora-no}" |
3230 | 3231 |
echo "libvo-aacenc support ${libvo_aacenc-no}" |
3231 | 3232 |
echo "libvo-amrwbenc support ${libvo_amrwbenc-no}" |
... | ... |
@@ -573,6 +573,7 @@ OBJS-$(CONFIG_LIBSCHROEDINGER_ENCODER) += libschroedingerenc.o \ |
573 | 573 |
libschroedinger.o \ |
574 | 574 |
libdirac_libschro.o |
575 | 575 |
OBJS-$(CONFIG_LIBSPEEX_DECODER) += libspeexdec.o |
576 |
+OBJS-$(CONFIG_LIBSTAGEFRIGHT_H264_DECODER)+= libstagefright.o |
|
576 | 577 |
OBJS-$(CONFIG_LIBTHEORA_ENCODER) += libtheoraenc.o |
577 | 578 |
OBJS-$(CONFIG_LIBVO_AACENC_ENCODER) += libvo-aacenc.o mpeg4audio.o |
578 | 579 |
OBJS-$(CONFIG_LIBVO_AMRWBENC_ENCODER) += libvo-amrwbenc.o |
... | ... |
@@ -369,6 +369,7 @@ void avcodec_register_all(void) |
369 | 369 |
REGISTER_DECODER (LIBOPENJPEG, libopenjpeg); |
370 | 370 |
REGISTER_ENCDEC (LIBSCHROEDINGER, libschroedinger); |
371 | 371 |
REGISTER_DECODER (LIBSPEEX, libspeex); |
372 |
+ REGISTER_DECODER (LIBSTAGEFRIGHT_H264, libstagefright_h264); |
|
372 | 373 |
REGISTER_ENCODER (LIBTHEORA, libtheora); |
373 | 374 |
REGISTER_ENCODER (LIBVO_AACENC, libvo_aacenc); |
374 | 375 |
REGISTER_ENCODER (LIBVO_AMRWBENC, libvo_amrwbenc); |
375 | 376 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,516 @@ |
0 |
+/* |
|
1 |
+ * Interface to the Android Stagefright library for |
|
2 |
+ * H/W accelerated H.264 decoding |
|
3 |
+ * |
|
4 |
+ * Copyright (C) 2011 Mohamed Naufal |
|
5 |
+ * Copyright (C) 2011 Martin Storsjö |
|
6 |
+ * |
|
7 |
+ * This file is part of Libav. |
|
8 |
+ * |
|
9 |
+ * Libav is free software; you can redistribute it and/or |
|
10 |
+ * modify it under the terms of the GNU Lesser General Public |
|
11 |
+ * License as published by the Free Software Foundation; either |
|
12 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
13 |
+ * |
|
14 |
+ * Libav is distributed in the hope that it will be useful, |
|
15 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
17 |
+ * Lesser General Public License for more details. |
|
18 |
+ * |
|
19 |
+ * You should have received a copy of the GNU Lesser General Public |
|
20 |
+ * License along with Libav; if not, write to the Free Software |
|
21 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
22 |
+ */ |
|
23 |
+ |
|
24 |
+#include <binder/ProcessState.h> |
|
25 |
+#include <media/stagefright/MetaData.h> |
|
26 |
+#include <media/stagefright/MediaBufferGroup.h> |
|
27 |
+#include <media/stagefright/MediaDebug.h> |
|
28 |
+#include <media/stagefright/MediaDefs.h> |
|
29 |
+#include <media/stagefright/OMXClient.h> |
|
30 |
+#include <media/stagefright/OMXCodec.h> |
|
31 |
+#include <utils/List.h> |
|
32 |
+#include <new> |
|
33 |
+ |
|
34 |
+extern "C" { |
|
35 |
+#include "avcodec.h" |
|
36 |
+#include "libavutil/imgutils.h" |
|
37 |
+} |
|
38 |
+ |
|
39 |
+#define OMX_QCOM_COLOR_FormatYVU420SemiPlanar 0x7FA30C00 |
|
40 |
+ |
|
41 |
+using namespace android; |
|
42 |
+ |
|
43 |
+struct Frame { |
|
44 |
+ status_t status; |
|
45 |
+ size_t size; |
|
46 |
+ int64_t time; |
|
47 |
+ int key; |
|
48 |
+ uint8_t *buffer; |
|
49 |
+ MediaBuffer* mbuffer; |
|
50 |
+ int32_t w, h; |
|
51 |
+}; |
|
52 |
+ |
|
53 |
+class CustomSource; |
|
54 |
+ |
|
55 |
+struct StagefrightContext { |
|
56 |
+ AVCodecContext *avctx; |
|
57 |
+ AVBitStreamFilterContext *bsfc; |
|
58 |
+ uint8_t* orig_extradata; |
|
59 |
+ int orig_extradata_size; |
|
60 |
+ sp<MediaSource> source; |
|
61 |
+ List<Frame*> *in_queue, *out_queue; |
|
62 |
+ pthread_mutex_t in_mutex, out_mutex; |
|
63 |
+ pthread_cond_t condition; |
|
64 |
+ pthread_t decode_thread_id; |
|
65 |
+ |
|
66 |
+ Frame *end_frame; |
|
67 |
+ bool source_done; |
|
68 |
+ volatile sig_atomic_t thread_exited, stop_decode; |
|
69 |
+ |
|
70 |
+ AVFrame ret_frame; |
|
71 |
+ |
|
72 |
+ uint8_t *dummy_buf; |
|
73 |
+ int dummy_bufsize; |
|
74 |
+ |
|
75 |
+ OMXClient *client; |
|
76 |
+ sp<MediaSource> decoder; |
|
77 |
+ const char *decoder_component; |
|
78 |
+}; |
|
79 |
+ |
|
80 |
+class CustomSource : public MediaSource { |
|
81 |
+public: |
|
82 |
+ CustomSource(AVCodecContext *avctx, sp<MetaData> meta) { |
|
83 |
+ s = (StagefrightContext*)avctx->priv_data; |
|
84 |
+ source_meta = meta; |
|
85 |
+ frame_size = (avctx->width * avctx->height * 3) / 2; |
|
86 |
+ buf_group.add_buffer(new MediaBuffer(frame_size)); |
|
87 |
+ } |
|
88 |
+ |
|
89 |
+ virtual sp<MetaData> getFormat() { |
|
90 |
+ return source_meta; |
|
91 |
+ } |
|
92 |
+ |
|
93 |
+ virtual status_t start(MetaData *params) { |
|
94 |
+ return OK; |
|
95 |
+ } |
|
96 |
+ |
|
97 |
+ virtual status_t stop() { |
|
98 |
+ return OK; |
|
99 |
+ } |
|
100 |
+ |
|
101 |
+ virtual status_t read(MediaBuffer **buffer, |
|
102 |
+ const MediaSource::ReadOptions *options) { |
|
103 |
+ Frame *frame; |
|
104 |
+ status_t ret; |
|
105 |
+ |
|
106 |
+ pthread_mutex_lock(&s->in_mutex); |
|
107 |
+ |
|
108 |
+ while (s->in_queue->empty()) |
|
109 |
+ pthread_cond_wait(&s->condition, &s->in_mutex); |
|
110 |
+ |
|
111 |
+ frame = *s->in_queue->begin(); |
|
112 |
+ ret = frame->status; |
|
113 |
+ |
|
114 |
+ if (ret == OK) { |
|
115 |
+ ret = buf_group.acquire_buffer(buffer); |
|
116 |
+ if (ret == OK) { |
|
117 |
+ memcpy((*buffer)->data(), frame->buffer, frame->size); |
|
118 |
+ (*buffer)->set_range(0, frame->size); |
|
119 |
+ (*buffer)->meta_data()->clear(); |
|
120 |
+ (*buffer)->meta_data()->setInt32(kKeyIsSyncFrame,frame->key); |
|
121 |
+ (*buffer)->meta_data()->setInt64(kKeyTime, frame->time); |
|
122 |
+ } else { |
|
123 |
+ av_log(s->avctx, AV_LOG_ERROR, "Failed to acquire MediaBuffer\n"); |
|
124 |
+ } |
|
125 |
+ av_freep(&frame->buffer); |
|
126 |
+ } |
|
127 |
+ |
|
128 |
+ s->in_queue->erase(s->in_queue->begin()); |
|
129 |
+ pthread_mutex_unlock(&s->in_mutex); |
|
130 |
+ |
|
131 |
+ av_freep(&frame); |
|
132 |
+ return ret; |
|
133 |
+ } |
|
134 |
+ |
|
135 |
+private: |
|
136 |
+ MediaBufferGroup buf_group; |
|
137 |
+ sp<MetaData> source_meta; |
|
138 |
+ StagefrightContext *s; |
|
139 |
+ int frame_size; |
|
140 |
+}; |
|
141 |
+ |
|
142 |
+void* decode_thread(void *arg) |
|
143 |
+{ |
|
144 |
+ AVCodecContext *avctx = (AVCodecContext*)arg; |
|
145 |
+ StagefrightContext *s = (StagefrightContext*)avctx->priv_data; |
|
146 |
+ Frame* frame; |
|
147 |
+ MediaBuffer *buffer; |
|
148 |
+ int decode_done = 0; |
|
149 |
+ do { |
|
150 |
+ buffer = NULL; |
|
151 |
+ frame = (Frame*)av_mallocz(sizeof(Frame)); |
|
152 |
+ if (!frame) { |
|
153 |
+ frame = s->end_frame; |
|
154 |
+ frame->status = AVERROR(ENOMEM); |
|
155 |
+ decode_done = 1; |
|
156 |
+ s->end_frame = NULL; |
|
157 |
+ } else { |
|
158 |
+ frame->status = s->decoder->read(&buffer); |
|
159 |
+ if (frame->status == OK) { |
|
160 |
+ sp<MetaData> outFormat = s->decoder->getFormat(); |
|
161 |
+ outFormat->findInt32(kKeyWidth , &frame->w); |
|
162 |
+ outFormat->findInt32(kKeyHeight, &frame->h); |
|
163 |
+ frame->size = buffer->range_length(); |
|
164 |
+ frame->mbuffer = buffer; |
|
165 |
+ } else if (frame->status == INFO_FORMAT_CHANGED) { |
|
166 |
+ if (buffer) |
|
167 |
+ buffer->release(); |
|
168 |
+ av_free(frame); |
|
169 |
+ continue; |
|
170 |
+ } else { |
|
171 |
+ decode_done = 1; |
|
172 |
+ } |
|
173 |
+ } |
|
174 |
+ pthread_mutex_lock(&s->out_mutex); |
|
175 |
+ s->out_queue->push_back(frame); |
|
176 |
+ pthread_mutex_unlock(&s->out_mutex); |
|
177 |
+ } while (!decode_done && !s->stop_decode); |
|
178 |
+ |
|
179 |
+ s->thread_exited = true; |
|
180 |
+ |
|
181 |
+ return 0; |
|
182 |
+} |
|
183 |
+ |
|
184 |
+static av_cold int Stagefright_init(AVCodecContext *avctx) |
|
185 |
+{ |
|
186 |
+ StagefrightContext *s = (StagefrightContext*)avctx->priv_data; |
|
187 |
+ sp<MetaData> meta, outFormat; |
|
188 |
+ int32_t colorFormat = 0; |
|
189 |
+ int ret; |
|
190 |
+ |
|
191 |
+ if (!avctx->extradata || !avctx->extradata_size || avctx->extradata[0] != 1) |
|
192 |
+ return -1; |
|
193 |
+ |
|
194 |
+ s->avctx = avctx; |
|
195 |
+ s->bsfc = av_bitstream_filter_init("h264_mp4toannexb"); |
|
196 |
+ if (!s->bsfc) { |
|
197 |
+ av_log(avctx, AV_LOG_ERROR, "Cannot open the h264_mp4toannexb BSF!\n"); |
|
198 |
+ return -1; |
|
199 |
+ } |
|
200 |
+ |
|
201 |
+ s->orig_extradata_size = avctx->extradata_size; |
|
202 |
+ s->orig_extradata = (uint8_t*) av_mallocz(avctx->extradata_size + |
|
203 |
+ FF_INPUT_BUFFER_PADDING_SIZE); |
|
204 |
+ if (!s->orig_extradata) { |
|
205 |
+ ret = AVERROR(ENOMEM); |
|
206 |
+ goto fail; |
|
207 |
+ } |
|
208 |
+ memcpy(s->orig_extradata, avctx->extradata, avctx->extradata_size); |
|
209 |
+ |
|
210 |
+ meta = new MetaData; |
|
211 |
+ if (meta == NULL) { |
|
212 |
+ ret = AVERROR(ENOMEM); |
|
213 |
+ goto fail; |
|
214 |
+ } |
|
215 |
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); |
|
216 |
+ meta->setInt32(kKeyWidth, avctx->width); |
|
217 |
+ meta->setInt32(kKeyHeight, avctx->height); |
|
218 |
+ meta->setData(kKeyAVCC, kTypeAVCC, avctx->extradata, avctx->extradata_size); |
|
219 |
+ |
|
220 |
+ android::ProcessState::self()->startThreadPool(); |
|
221 |
+ |
|
222 |
+ s->source = new CustomSource(avctx, meta); |
|
223 |
+ s->in_queue = new List<Frame*>; |
|
224 |
+ s->out_queue = new List<Frame*>; |
|
225 |
+ s->client = new OMXClient; |
|
226 |
+ s->end_frame = (Frame*)av_mallocz(sizeof(Frame)); |
|
227 |
+ if (s->source == NULL || !s->in_queue || !s->out_queue || !s->client || |
|
228 |
+ !s->end_frame) { |
|
229 |
+ ret = AVERROR(ENOMEM); |
|
230 |
+ goto fail; |
|
231 |
+ } |
|
232 |
+ |
|
233 |
+ if (s->client->connect() != OK) { |
|
234 |
+ av_log(avctx, AV_LOG_ERROR, "Cannot connect OMX client\n"); |
|
235 |
+ ret = -1; |
|
236 |
+ goto fail; |
|
237 |
+ } |
|
238 |
+ |
|
239 |
+ s->decoder = OMXCodec::Create(s->client->interface(), meta, |
|
240 |
+ false, s->source, NULL, |
|
241 |
+ OMXCodec::kClientNeedsFramebuffer); |
|
242 |
+ if (s->decoder->start() != OK) { |
|
243 |
+ av_log(avctx, AV_LOG_ERROR, "Cannot start decoder\n"); |
|
244 |
+ ret = -1; |
|
245 |
+ s->client->disconnect(); |
|
246 |
+ goto fail; |
|
247 |
+ } |
|
248 |
+ |
|
249 |
+ outFormat = s->decoder->getFormat(); |
|
250 |
+ outFormat->findInt32(kKeyColorFormat, &colorFormat); |
|
251 |
+ if (colorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar || |
|
252 |
+ colorFormat == OMX_COLOR_FormatYUV420SemiPlanar) |
|
253 |
+ avctx->pix_fmt = PIX_FMT_NV21; |
|
254 |
+ else |
|
255 |
+ avctx->pix_fmt = PIX_FMT_YUV420P; |
|
256 |
+ |
|
257 |
+ outFormat->findCString(kKeyDecoderComponent, &s->decoder_component); |
|
258 |
+ if (s->decoder_component) |
|
259 |
+ s->decoder_component = av_strdup(s->decoder_component); |
|
260 |
+ |
|
261 |
+ pthread_mutex_init(&s->in_mutex, NULL); |
|
262 |
+ pthread_mutex_init(&s->out_mutex, NULL); |
|
263 |
+ pthread_cond_init(&s->condition, NULL); |
|
264 |
+ pthread_create(&s->decode_thread_id, NULL, &decode_thread, avctx); |
|
265 |
+ return 0; |
|
266 |
+ |
|
267 |
+fail: |
|
268 |
+ av_bitstream_filter_close(s->bsfc); |
|
269 |
+ av_freep(&s->orig_extradata); |
|
270 |
+ av_freep(&s->end_frame); |
|
271 |
+ delete s->in_queue; |
|
272 |
+ delete s->out_queue; |
|
273 |
+ delete s->client; |
|
274 |
+ return ret; |
|
275 |
+} |
|
276 |
+ |
|
277 |
+static int Stagefright_decode_frame(AVCodecContext *avctx, void *data, |
|
278 |
+ int *data_size, AVPacket *avpkt) |
|
279 |
+{ |
|
280 |
+ StagefrightContext *s = (StagefrightContext*)avctx->priv_data; |
|
281 |
+ Frame *frame; |
|
282 |
+ MediaBuffer *mbuffer; |
|
283 |
+ status_t status; |
|
284 |
+ size_t size; |
|
285 |
+ uint8_t *buf; |
|
286 |
+ const uint8_t *src_data[3]; |
|
287 |
+ int w, h; |
|
288 |
+ int src_linesize[3]; |
|
289 |
+ int orig_size = avpkt->size; |
|
290 |
+ AVPacket pkt = *avpkt; |
|
291 |
+ int ret; |
|
292 |
+ |
|
293 |
+ if (avpkt && avpkt->data) { |
|
294 |
+ av_bitstream_filter_filter(s->bsfc, avctx, NULL, &pkt.data, &pkt.size, |
|
295 |
+ avpkt->data, avpkt->size, avpkt->flags & AV_PKT_FLAG_KEY); |
|
296 |
+ avpkt = &pkt; |
|
297 |
+ } |
|
298 |
+ |
|
299 |
+ if (!s->source_done) { |
|
300 |
+ if(!s->dummy_buf) { |
|
301 |
+ s->dummy_buf = (uint8_t*)av_malloc(avpkt->size); |
|
302 |
+ if (!s->dummy_buf) |
|
303 |
+ return AVERROR(ENOMEM); |
|
304 |
+ s->dummy_bufsize = avpkt->size; |
|
305 |
+ memcpy(s->dummy_buf, avpkt->data, avpkt->size); |
|
306 |
+ } |
|
307 |
+ |
|
308 |
+ frame = (Frame*)av_mallocz(sizeof(Frame)); |
|
309 |
+ if (avpkt->data) { |
|
310 |
+ frame->status = OK; |
|
311 |
+ frame->size = orig_size; |
|
312 |
+ // Stagefright can't handle negative timestamps - |
|
313 |
+ // if needed, work around this by offsetting them manually? |
|
314 |
+ if (avpkt->pts >= 0) |
|
315 |
+ frame->time = avpkt->pts; |
|
316 |
+ frame->key = avpkt->flags & AV_PKT_FLAG_KEY ? 1 : 0; |
|
317 |
+ frame->buffer = (uint8_t*)av_malloc(avpkt->size); |
|
318 |
+ if (!frame->buffer) { |
|
319 |
+ av_freep(&frame); |
|
320 |
+ return AVERROR(ENOMEM); |
|
321 |
+ } |
|
322 |
+ uint8_t *ptr = avpkt->data; |
|
323 |
+ // The OMX.SEC decoder fails without this. |
|
324 |
+ if (avpkt->size == orig_size + avctx->extradata_size) |
|
325 |
+ ptr += avctx->extradata_size; |
|
326 |
+ memcpy(frame->buffer, ptr, orig_size); |
|
327 |
+ } else { |
|
328 |
+ frame->status = ERROR_END_OF_STREAM; |
|
329 |
+ s->source_done = true; |
|
330 |
+ } |
|
331 |
+ |
|
332 |
+ while (true) { |
|
333 |
+ if (s->thread_exited) { |
|
334 |
+ s->source_done = true; |
|
335 |
+ break; |
|
336 |
+ } |
|
337 |
+ pthread_mutex_lock(&s->in_mutex); |
|
338 |
+ if (s->in_queue->size() >= 10) { |
|
339 |
+ pthread_mutex_unlock(&s->in_mutex); |
|
340 |
+ usleep(10000); |
|
341 |
+ continue; |
|
342 |
+ } |
|
343 |
+ s->in_queue->push_back(frame); |
|
344 |
+ pthread_cond_signal(&s->condition); |
|
345 |
+ pthread_mutex_unlock(&s->in_mutex); |
|
346 |
+ break; |
|
347 |
+ } |
|
348 |
+ } |
|
349 |
+ while (true) { |
|
350 |
+ pthread_mutex_lock(&s->out_mutex); |
|
351 |
+ if (!s->out_queue->empty()) break; |
|
352 |
+ pthread_mutex_unlock(&s->out_mutex); |
|
353 |
+ if (s->source_done) { |
|
354 |
+ usleep(10000); |
|
355 |
+ continue; |
|
356 |
+ } else { |
|
357 |
+ return orig_size; |
|
358 |
+ } |
|
359 |
+ } |
|
360 |
+ |
|
361 |
+ frame = *s->out_queue->begin(); |
|
362 |
+ s->out_queue->erase(s->out_queue->begin()); |
|
363 |
+ pthread_mutex_unlock(&s->out_mutex); |
|
364 |
+ |
|
365 |
+ mbuffer = frame->mbuffer; |
|
366 |
+ status = frame->status; |
|
367 |
+ size = frame->size; |
|
368 |
+ w = frame->w; |
|
369 |
+ h = frame->h; |
|
370 |
+ av_freep(&frame); |
|
371 |
+ |
|
372 |
+ if (status == ERROR_END_OF_STREAM) |
|
373 |
+ return 0; |
|
374 |
+ if (status != OK) { |
|
375 |
+ if (status == AVERROR(ENOMEM)) |
|
376 |
+ return status; |
|
377 |
+ av_log(avctx, AV_LOG_ERROR, "Decode failed: %x\n", status); |
|
378 |
+ return -1; |
|
379 |
+ } |
|
380 |
+ |
|
381 |
+ // The OMX.SEC decoder doesn't signal the modified width/height |
|
382 |
+ if (s->decoder_component && !strncmp(s->decoder_component, "OMX.SEC", 7) && |
|
383 |
+ (w & 15 || h & 15)) { |
|
384 |
+ if (((w + 15)&~15) * ((h + 15)&~15) * 3/2 == size) { |
|
385 |
+ w = (w + 15)&~15; |
|
386 |
+ h = (h + 15)&~15; |
|
387 |
+ } |
|
388 |
+ } |
|
389 |
+ |
|
390 |
+ if (!avctx->width || !avctx->height || avctx->width > w || avctx->height > h) { |
|
391 |
+ avctx->width = w; |
|
392 |
+ avctx->height = h; |
|
393 |
+ } |
|
394 |
+ |
|
395 |
+ ret = avctx->reget_buffer(avctx, &s->ret_frame); |
|
396 |
+ if (ret < 0) { |
|
397 |
+ av_log(avctx, AV_LOG_ERROR, "reget buffer() failed\n"); |
|
398 |
+ goto end; |
|
399 |
+ } |
|
400 |
+ |
|
401 |
+ src_linesize[0] = w; |
|
402 |
+ if (avctx->pix_fmt == PIX_FMT_YUV420P) |
|
403 |
+ src_linesize[1] = src_linesize[2] = w/2; |
|
404 |
+ else if (avctx->pix_fmt == PIX_FMT_NV21) |
|
405 |
+ src_linesize[1] = w; |
|
406 |
+ |
|
407 |
+ buf = (uint8_t*)mbuffer->data(); |
|
408 |
+ src_data[0] = buf; |
|
409 |
+ src_data[1] = buf + src_linesize[0] * h; |
|
410 |
+ src_data[2] = src_data[1] + src_linesize[1] * h/2; |
|
411 |
+ av_image_copy(s->ret_frame.data, s->ret_frame.linesize, |
|
412 |
+ src_data, src_linesize, |
|
413 |
+ avctx->pix_fmt, avctx->width, avctx->height); |
|
414 |
+ |
|
415 |
+ *data_size = sizeof(AVFrame); |
|
416 |
+ *(AVFrame*)data = s->ret_frame; |
|
417 |
+ ret = orig_size; |
|
418 |
+end: |
|
419 |
+ mbuffer->release(); |
|
420 |
+ return ret; |
|
421 |
+} |
|
422 |
+ |
|
423 |
+static av_cold int Stagefright_close(AVCodecContext *avctx) |
|
424 |
+{ |
|
425 |
+ StagefrightContext *s = (StagefrightContext*)avctx->priv_data; |
|
426 |
+ Frame *frame; |
|
427 |
+ |
|
428 |
+ if (!s->thread_exited) { |
|
429 |
+ s->stop_decode = 1; |
|
430 |
+ |
|
431 |
+ // Feed a dummy frame prior to signalling EOF. |
|
432 |
+ // This is required to terminate the decoder(OMX.SEC) |
|
433 |
+ // when only one frame is read during stream info detection. |
|
434 |
+ if (s->dummy_buf && (frame = (Frame*)av_mallocz(sizeof(Frame)))) { |
|
435 |
+ frame->status = OK; |
|
436 |
+ frame->size = s->dummy_bufsize; |
|
437 |
+ frame->buffer = s->dummy_buf; |
|
438 |
+ pthread_mutex_lock(&s->in_mutex); |
|
439 |
+ s->in_queue->push_back(frame); |
|
440 |
+ pthread_cond_signal(&s->condition); |
|
441 |
+ pthread_mutex_unlock(&s->in_mutex); |
|
442 |
+ s->dummy_buf = NULL; |
|
443 |
+ } |
|
444 |
+ |
|
445 |
+ pthread_mutex_lock(&s->in_mutex); |
|
446 |
+ s->end_frame->status = ERROR_END_OF_STREAM; |
|
447 |
+ s->in_queue->push_back(s->end_frame); |
|
448 |
+ pthread_cond_signal(&s->condition); |
|
449 |
+ pthread_mutex_unlock(&s->in_mutex); |
|
450 |
+ s->end_frame = NULL; |
|
451 |
+ } |
|
452 |
+ |
|
453 |
+ pthread_join(s->decode_thread_id, NULL); |
|
454 |
+ |
|
455 |
+ if (s->ret_frame.data[0]) |
|
456 |
+ avctx->release_buffer(avctx, &s->ret_frame); |
|
457 |
+ |
|
458 |
+ while (!s->in_queue->empty()) { |
|
459 |
+ frame = *s->in_queue->begin(); |
|
460 |
+ s->in_queue->erase(s->in_queue->begin()); |
|
461 |
+ if (frame->size) |
|
462 |
+ av_freep(&frame->buffer); |
|
463 |
+ av_freep(&frame); |
|
464 |
+ } |
|
465 |
+ |
|
466 |
+ while (!s->out_queue->empty()) { |
|
467 |
+ frame = *s->out_queue->begin(); |
|
468 |
+ s->out_queue->erase(s->out_queue->begin()); |
|
469 |
+ if (frame->size) |
|
470 |
+ frame->mbuffer->release(); |
|
471 |
+ av_freep(&frame); |
|
472 |
+ } |
|
473 |
+ |
|
474 |
+ s->decoder->stop(); |
|
475 |
+ s->client->disconnect(); |
|
476 |
+ |
|
477 |
+ if (s->decoder_component) |
|
478 |
+ av_freep(&s->decoder_component); |
|
479 |
+ av_freep(&s->dummy_buf); |
|
480 |
+ av_freep(&s->end_frame); |
|
481 |
+ |
|
482 |
+ // Reset the extradata back to the original mp4 format, so that |
|
483 |
+ // the next invocation (both when decoding and when called from |
|
484 |
+ // av_find_stream_info) get the original mp4 format extradata. |
|
485 |
+ av_freep(&avctx->extradata); |
|
486 |
+ avctx->extradata = s->orig_extradata; |
|
487 |
+ avctx->extradata_size = s->orig_extradata_size; |
|
488 |
+ |
|
489 |
+ delete s->in_queue; |
|
490 |
+ delete s->out_queue; |
|
491 |
+ delete s->client; |
|
492 |
+ |
|
493 |
+ pthread_mutex_destroy(&s->in_mutex); |
|
494 |
+ pthread_mutex_destroy(&s->out_mutex); |
|
495 |
+ pthread_cond_destroy(&s->condition); |
|
496 |
+ av_bitstream_filter_close(s->bsfc); |
|
497 |
+ return 0; |
|
498 |
+} |
|
499 |
+ |
|
500 |
+AVCodec ff_libstagefright_h264_decoder = { |
|
501 |
+ "libstagefright_h264", |
|
502 |
+ AVMEDIA_TYPE_VIDEO, |
|
503 |
+ CODEC_ID_H264, |
|
504 |
+ sizeof(StagefrightContext), |
|
505 |
+ Stagefright_init, |
|
506 |
+ NULL, //encode |
|
507 |
+ Stagefright_close, |
|
508 |
+ Stagefright_decode_frame, |
|
509 |
+ CODEC_CAP_DELAY, |
|
510 |
+ NULL, //next |
|
511 |
+ NULL, //flush |
|
512 |
+ NULL, //supported_framerates |
|
513 |
+ NULL, //pixel_formats |
|
514 |
+ NULL_IF_CONFIG_SMALL("libstagefright H.264"), |
|
515 |
+}; |
... | ... |
@@ -21,7 +21,7 @@ |
21 | 21 |
#define AVCODEC_VERSION_H |
22 | 22 |
|
23 | 23 |
#define LIBAVCODEC_VERSION_MAJOR 53 |
24 |
-#define LIBAVCODEC_VERSION_MINOR 9 |
|
24 |
+#define LIBAVCODEC_VERSION_MINOR 10 |
|
25 | 25 |
#define LIBAVCODEC_VERSION_MICRO 0 |
26 | 26 |
|
27 | 27 |
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ |