1 /*
2 * Android MediaCodec decoder
3 *
4 * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <string.h>
24 #include <sys/types.h>
25
33
36
42
43 /**
44 * OMX.k3.video.decoder.avc, OMX.NVIDIA.* OMX.SEC.avc.dec and OMX.google
45 * codec workarounds used in various place are taken from the Gstreamer
46 * project.
47 *
48 * Gstreamer references:
49 * https://cgit.freedesktop.org/gstreamer/gst-plugins-bad/tree/sys/androidmedia/
50 *
51 * Gstreamer copyright notice:
52 *
53 * Copyright (C) 2012, Collabora Ltd.
54 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
55 *
56 * Copyright (C) 2012, Rafaël Carré <funman@videolanorg>
57 *
58 * Copyright (C) 2015, Sebastian Dröge <sebastian@centricular.com>
59 *
60 * Copyright (C) 2014-2015, Collabora Ltd.
61 * Author: Matthieu Bouron <matthieu.bouron@gcollabora.com>
62 *
63 * Copyright (C) 2015, Edward Hervey
64 * Author: Edward Hervey <bilboed@gmail.com>
65 *
66 * Copyright (C) 2015, Matthew Waters <matthew@centricular.com>
67 *
68 * This library is free software; you can redistribute it and/or
69 * modify it under the terms of the GNU Lesser General Public
70 * License as published by the Free Software Foundation
71 * version 2.1 of the License.
72 *
73 * This library is distributed in the hope that it will be useful,
74 * but WITHOUT ANY WARRANTY; without even the implied warranty of
75 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
76 * Lesser General Public License for more details.
77 *
78 * You should have received a copy of the GNU Lesser General Public
79 * License along with this library; if not, write to the Free Software
80 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
81 *
82 */
83
84 #define INPUT_DEQUEUE_TIMEOUT_US 8000
85 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
86 #define OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US 1000000
87
88 enum {
91 };
92
94 {
100 default:
102 }
103 }
104
105 enum {
110 };
111
113 {
114 switch (color_standard) {
123 default:
125 }
126 }
127
129 {
130 switch (color_standard) {
139 default:
141 }
142 }
143
144 enum {
149 };
150
152 {
153 switch (color_transfer) {
162 default:
164 }
165 }
166
167 enum {
177 };
178
179 static const struct {
180
183
185
193 { 0 }
194 };
195
199 {
202
205 }
206
209 }
210
214 }
215 }
216
219
221 }
222
224 {
226 }
227
229 {
231 return;
232
237 }
238
242 }
243
247 }
248
251 }
252 }
253
255 {
256 AVMediaCodecBuffer *
buffer = opaque;
259
263 "Releasing output buffer %zd (%p) ts=%"PRId64" on free() [%d pending]\n",
266 }
267
268 if (
ctx->delay_flush)
271 }
272
278 {
282
288
293 } else {
295 }
301
306 }
307
309
311 0,
315
316 if (!
frame->buf[0]) {
319
320 }
321
326
329
331
334 "Wrapping output buffer %zd (%p) ts=%"PRId64" [%d pending]\n",
336
337 return 0;
345 }
346
348 }
349
357 {
360
364
365 /* MediaCodec buffers needs to be copied to our own refcounted buffers
366 * because the flush command invalidates all input and output buffers.
367 */
370 goto done;
371 }
372
373 /* Override frame->pkt_pts as ff_get_buffer will override its value based
374 * on the last avpacket received which is not in sync with the frame:
375 * * N avpackets can be pushed before 1 frame is actually returned
376 * * 0-sized avpackets are pushed to flush remaining frames at EOS */
381 } else {
383 }
385
387 "Frame: width=%d stride=%d height=%d slice-height=%d "
388 "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s "
389 "destination linesizes=%d,%d,%d\n" ,
391 s->crop_top,
s->crop_bottom,
s->crop_left,
s->crop_right,
s->codec_name,
393
394 switch (
s->color_format) {
397 break;
402 break;
406 break;
409 break;
410 default:
412 s->color_format,
s->color_format);
414 goto done;
415 }
416
418 done:
423 }
424
426 }
427
428 #define AMEDIAFORMAT_GET_INT32(name, key, mandatory) do { \
429 int32_t value = 0; \
430 if (ff_AMediaFormat_getInt32(s->format, key, &value)) { \
431 (name) = value; \
432 } else if (mandatory) { \
433 av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", key, format); \
434 ret = AVERROR_EXTERNAL; \
435 goto fail; \
436 } \
437 } while (0) \
438
440 {
445 int color_standard = 0;
446 int color_transfer = 0;
448
452 }
453
457 }
459
460 /* Mandatory fields */
463
465 s->stride =
s->stride > 0 ?
s->stride :
s->width;
466
468
469 if (strstr(
s->codec_name,
"OMX.Nvidia.") &&
s->slice_height == 0) {
470 s->slice_height =
FFALIGN(
s->height, 16);
471 }
else if (strstr(
s->codec_name,
"OMX.SEC.avc.dec")) {
472 s->slice_height = avctx->
height;
474 }
else if (
s->slice_height == 0) {
475 s->slice_height =
s->height;
476 }
477
484 }
485
486 /* Optional fields */
491
492 width =
s->crop_right + 1 -
s->crop_left;
493 height =
s->crop_bottom + 1 -
s->crop_top;
494
497
498 if (
s->display_width &&
s->display_height) {
503 }
504
508
510 if (color_standard) {
513 }
514
516 if (color_transfer)
518
520 "Output crop parameters top=%d bottom=%d left=%d right=%d, "
521 "resulting dimensions width=%d height=%d\n",
522 s->crop_top,
s->crop_bottom,
s->crop_left,
s->crop_right,
524
530 }
531
533 {
536
537 s->output_buffer_count = 0;
538
544 s->current_input_buffer = -1;
545
550 }
551
552 return 0;
553 }
554
557 {
561
566 };
567
572 s->current_input_buffer = -1;
573
577
581 if (device_ctx->
hwctx) {
585 }
586 }
587 }
588
589 if (!
s->surface && user_ctx && user_ctx->
surface) {
592 }
593 }
594
598 }
599
601 if (!
s->codec_name) {
604 }
605
609 av_log(avctx,
AV_LOG_ERROR,
"Failed to create media decoder for type %s and name %s\n", mime,
s->codec_name);
612 }
613
618 "Failed to configure codec %s (status = %d) with format %s\n",
621
624 }
625
630 "Failed to start codec %s (status = %d) with format %s\n",
635 }
636
641 "Failed to configure context\n");
643 }
644 }
645
647
648 return 0;
649
654 }
655
658 {
660 int need_draining = 0;
667
670 "until all output buffers have been released\n");
672 }
673
675 need_draining = 1;
676 }
677
678 if (
s->draining &&
s->eos) {
680 }
681
682 while (offset < pkt->
size || (need_draining && !
s->draining)) {
683 ssize_t
index =
s->current_input_buffer;
688 break;
689 }
690
694 }
695 }
696 s->current_input_buffer = -1;
697
702 }
703
708 }
711 }
712
713 if (need_draining) {
715
717
722 }
723
725 "Queued empty EOS input buffer %zd with flags=%d\n",
index,
flags);
726
728 return 0;
729 }
730
734
739 }
740
742 "Queued input buffer %zd size=%zd ts=%"PRIi64
"\n",
index,
size,
pts);
743 }
744
748 }
749
752 {
761
762 if (
s->draining &&
s->eos) {
764 }
765
767 /* If the codec is flushing or need to be flushed, block for a fair
768 * amount of time to ensure we got a frame */
770 }
else if (
s->output_buffer_count == 0 || !wait) {
771 /* If the codec hasn't produced any frames, do not block so we
772 * can push data to it as fast as possible, and get the first
773 * frame */
774 output_dequeue_timeout_us = 0;
775 }
776
780 " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
782 info.presentationTimeUs,
info.flags);
783
786 }
787
793 }
794 } else {
799 }
800
804 }
805 }
806
807 s->output_buffer_count++;
808 return 0;
809 } else {
813 }
814 }
815
818
823 }
824 }
825
830 }
831
835 }
838
841 }
842
848 "while draining remaining frames, output will probably lack frames\n",
849 output_dequeue_timeout_us / 1000);
850 } else {
852 }
853 } else {
856 }
857
859 }
860
861 /*
862 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
863 * the codec (because the user retains frames). The codec stays in the
864 * flushing state.
865 *
866 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
867 * performed on the codec. The codec leaves the flushing state and can
868 * process again packets.
869 *
870 * ff_mediacodec_dec_flush returns a negative value if an error has
871 * occurred.
872 */
874 {
877
878 /* No frames (holding a reference to the codec) are retained by the
879 * user, thus we can flush the codec and returns accordingly */
882 }
883
884 return 1;
885 }
886
888 return 0;
889 }
890
892 {
894
895 return 0;
896 }
897
899 {
901 }