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
35
38
44
45 /**
46 * OMX.k3.video.decoder.avc, OMX.NVIDIA.* OMX.SEC.avc.dec and OMX.google
47 * codec workarounds used in various place are taken from the Gstreamer
48 * project.
49 *
50 * Gstreamer references:
51 * https://cgit.freedesktop.org/gstreamer/gst-plugins-bad/tree/sys/androidmedia/
52 *
53 * Gstreamer copyright notice:
54 *
55 * Copyright (C) 2012, Collabora Ltd.
56 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
57 *
58 * Copyright (C) 2012, Rafaël Carré <funman@videolanorg>
59 *
60 * Copyright (C) 2015, Sebastian Dröge <sebastian@centricular.com>
61 *
62 * Copyright (C) 2014-2015, Collabora Ltd.
63 * Author: Matthieu Bouron <matthieu.bouron@gcollabora.com>
64 *
65 * Copyright (C) 2015, Edward Hervey
66 * Author: Edward Hervey <bilboed@gmail.com>
67 *
68 * Copyright (C) 2015, Matthew Waters <matthew@centricular.com>
69 *
70 * This library is free software; you can redistribute it and/or
71 * modify it under the terms of the GNU Lesser General Public
72 * License as published by the Free Software Foundation
73 * version 2.1 of the License.
74 *
75 * This library is distributed in the hope that it will be useful,
76 * but WITHOUT ANY WARRANTY; without even the implied warranty of
77 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
78 * Lesser General Public License for more details.
79 *
80 * You should have received a copy of the GNU Lesser General Public
81 * License along with this library; if not, write to the Free Software
82 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
83 *
84 */
85
86 #define INPUT_DEQUEUE_TIMEOUT_US 8000
87 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
88 #define OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US 1000000
89
90 enum {
96 };
97
98 static const struct {
99
102
104
109 { 0 }
110 };
111
115 {
117
121 }
122 }
123
124 av_log(avctx,
AV_LOG_ERROR,
"Output sample format 0x%x (value=%d) is not supported\n",
126
128 }
129
130 enum
131 {
150 };
151
152 static const struct {
153
156
176 };
177
179 int channel_mask)
180 {
181 uint64_t channel_layout = 0;
182
186 }
187
188 return channel_layout;
189 }
190
191 enum {
201 };
202
203 static const struct {
204
207
209
217 { 0 }
218 };
219
223 {
226
229 }
230
233 }
234
238 }
239 }
240
243
245 }
246
248 {
250 }
251
253 {
255 return;
256
261 }
262
266 }
267
271 }
272
275 }
276 }
277
279 {
280 AVMediaCodecBuffer *
buffer = opaque;
283
287 "Releasing output buffer %zd (%p) ts=%"PRId64" on free() [%d pending]\n",
290 }
291
294 }
295
301 {
305
311
316 } else {
318 }
324
329 }
330
332
334 0,
338
339 if (!
frame->buf[0]) {
342
343 }
344
348
351
353
356 "Wrapping output buffer %zd (%p) ts=%"PRId64" [%d pending]\n",
358
359 return 0;
366 }
367
369 }
370
378 {
382 if (!sample_size) {
385 goto done;
386 }
387
391
395 goto done;
396 }
397
398 /* MediaCodec buffers needs to be copied to our own refcounted buffers
399 * because the flush command invalidates all input and output buffers.
400 */
404 goto done;
405 }
406
407 /* Override frame->pts as ff_get_buffer will override its value based
408 * on the last avpacket received which is not in sync with the frame:
409 * * N avpackets can be pushed before 1 frame is actually returned
410 * * 0-sized avpackets are pushed to flush remaining frames at EOS */
415 } else {
417 }
420
422 "Frame: format=%d channels=%d sample_rate=%d nb_samples=%d",
424
426
428 done:
433 }
434
436 }
437
445 {
448
452
453 /* MediaCodec buffers needs to be copied to our own refcounted buffers
454 * because the flush command invalidates all input and output buffers.
455 */
458 goto done;
459 }
460
461 /* Override frame->pkt_pts as ff_get_buffer will override its value based
462 * on the last avpacket received which is not in sync with the frame:
463 * * N avpackets can be pushed before 1 frame is actually returned
464 * * 0-sized avpackets are pushed to flush remaining frames at EOS */
469 } else {
471 }
473
475 "Frame: width=%d stride=%d height=%d slice-height=%d "
476 "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s "
477 "destination linesizes=%d,%d,%d\n" ,
479 s->crop_top,
s->crop_bottom,
s->crop_left,
s->crop_right,
s->codec_name,
481
482 switch (
s->color_format) {
485 break;
490 break;
494 break;
497 break;
498 default:
500 s->color_format,
s->color_format);
502 goto done;
503 }
504
506 done:
511 }
512
514 }
515
523 {
528 else
530 }
531
532 #define AMEDIAFORMAT_GET_INT32(name, key, mandatory) do { \
533 int32_t value = 0; \
534 if (ff_AMediaFormat_getInt32(s->format, key, &value)) { \
535 (name) = value; \
536 } else if (mandatory) { \
537 av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", key, format); \
538 ret = AVERROR_EXTERNAL; \
539 goto fail; \
540 } \
541 } while (0) \
542
544 {
549 int color_standard = 0;
550 int color_transfer = 0;
552
556 }
557
561 }
563
564 /* Mandatory fields */
567
569 s->stride =
s->stride > 0 ?
s->stride :
s->width;
570
572
573 if (strstr(
s->codec_name,
"OMX.Nvidia.") &&
s->slice_height == 0) {
574 s->slice_height =
FFALIGN(
s->height, 16);
575 }
else if (strstr(
s->codec_name,
"OMX.SEC.avc.dec")) {
576 s->slice_height = avctx->
height;
578 }
else if (
s->slice_height == 0) {
579 s->slice_height =
s->height;
580 }
581
588 }
589
590 /* Optional fields */
595
596 // Try "crop" for NDK
597 // MediaTek SOC return some default value like Rect(0, 0, 318, 238)
598 if (!(
s->crop_right &&
s->crop_bottom) &&
s->use_ndk_codec && !strstr(
s->codec_name,
".mtk."))
600
601 if (
s->crop_right &&
s->crop_bottom) {
602 width =
s->crop_right + 1 -
s->crop_left;
603 height =
s->crop_bottom + 1 -
s->crop_top;
604 } else {
605 /* TODO: NDK MediaFormat should try getRect() first.
606 * Try crop-width/crop-height, it works on NVIDIA Shield.
607 */
610 }
614 }
615
618
619 if (
s->display_width &&
s->display_height) {
624 }
625
629
631 if (color_standard) {
634 }
635
637 if (color_transfer)
639
641 "Output crop parameters top=%d bottom=%d left=%d right=%d, "
642 "resulting dimensions width=%d height=%d\n",
643 s->crop_top,
s->crop_bottom,
s->crop_left,
s->crop_right,
645
651 }
652
654 {
656 int sample_rate = 0;
657 int channel_count = 0;
658 int channel_mask = 0;
659 int pcm_encoding = 0;
661
665 }
666
670 }
672
673 /* Mandatory fields */
676
678 if (pcm_encoding)
680 else
682
684
686 if (channel_mask)
688 else
690
692 "Output parameters channel-count=%d channel-layout=%x sample-rate=%d\n",
693 channel_count, channel_mask, sample_rate);
694
698 }
699
701 {
706 else
708 }
709
711 {
714
715 s->output_buffer_count = 0;
716
722 s->current_input_buffer = -1;
723
728 }
729
730 return 0;
731 }
732
735 {
737
742 };
743
747
751 if (device_ctx->
hwctx) {
755 }
756 }
757 }
758
759 if (!
s->surface && user_ctx && user_ctx->
surface) {
762 }
763 }
764
768 }
769
771 if (!
s->codec_name) {
772 // getCodecNameByType() can fail due to missing JVM, while NDK
773 // mediacodec can be used without JVM.
774 if (!
s->use_ndk_codec) {
776 }
778 } else {
780 }
781
784 else {
790 }
791 }
793 av_log(avctx,
AV_LOG_ERROR,
"Failed to create media decoder for type %s and name %s\n", mime,
s->codec_name);
795 }
796
797 return 0;
798 }
799
802 {
807 }
808
810 if (!
s->codec_name) {
814 }
815
816 return 0;
817 }
818
821 {
824
829 s->current_input_buffer = -1;
830
835 else
839
844 "Failed to configure codec %s (status = %d) with format %s\n",
847
850 }
851
856 "Failed to start codec %s (status = %d) with format %s\n",
861 }
862
868 "Failed to configure context\n");
870 }
871 }
872 }
873
875
876 return 0;
877
882 }
883
886 {
888 int need_draining = 0;
895
898 "until all output buffers have been released\n");
900 }
901
903 need_draining = 1;
904 }
905
906 if (
s->draining &&
s->eos) {
908 }
909
910 while (offset < pkt->
size || (need_draining && !
s->draining)) {
911 ssize_t
index =
s->current_input_buffer;
916 break;
917 }
918
922 }
923 }
924 s->current_input_buffer = -1;
925
930 }
931
936 }
939 }
940
941 if (need_draining) {
943
945
950 }
951
953 "Queued empty EOS input buffer %zd with flags=%d\n",
index,
flags);
954
956 return 0;
957 }
958
962
967 }
968
970 "Queued input buffer %zd size=%zd ts=%"PRIi64
"\n",
index,
size,
pts);
971 }
972
976 }
977
980 {
989
990 if (
s->draining &&
s->eos) {
992 }
993
995 /* If the codec is flushing or need to be flushed, block for a fair
996 * amount of time to ensure we got a frame */
998 }
else if (
s->output_buffer_count == 0 || !wait) {
999 /* If the codec hasn't produced any frames, do not block so we
1000 * can push data to it as fast as possible, and get the first
1001 * frame */
1002 output_dequeue_timeout_us = 0;
1003 }
1004
1008 " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
1010 info.presentationTimeUs,
info.flags);
1011
1014 }
1015
1021 }
1022 } else {
1027 }
1028
1032 }
1033 }
1034
1035 s->output_buffer_count++;
1036 return 0;
1037 } else {
1041 }
1042 }
1043
1046
1051 }
1052 }
1053
1058 }
1059
1063 }
1066
1069 }
1070
1076 "while draining remaining frames, output will probably lack frames\n",
1077 output_dequeue_timeout_us / 1000);
1078 } else {
1080 }
1081 } else {
1084 }
1085
1086 if (
s->draining &&
s->eos)
1089 }
1090
1091 /*
1092 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
1093 * the codec (because the user retains frames). The codec stays in the
1094 * flushing state.
1095 *
1096 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
1097 * performed on the codec. The codec leaves the flushing state and can
1098 * process again packets.
1099 *
1100 * ff_mediacodec_dec_flush returns a negative value if an error has
1101 * occurred.
1102 */
1104 {
1105 if (!
s->surface || !
s->delay_flush ||
atomic_load(&
s->refcount) == 1) {
1107
1108 /* No frames (holding a reference to the codec) are retained by the
1109 * user, thus we can flush the codec and returns accordingly */
1112 }
1113
1114 return 1;
1115 }
1116
1118 return 0;
1119 }
1120
1122 {
1124 return 0;
1125
1130 } else {
1131 av_log(avctx,
AV_LOG_DEBUG,
"Not stopping MediaCodec (there are buffers pending)\n");
1132 }
1133 }
1134
1136
1137 return 0;
1138 }
1139
1141 {
1143 }