| @@ -23,6 +23,7 @@ · 22 unmodified lines |
| 23 | 23 | #include <string.h> |
| 24 | 24 | #include <sys/types.h> |
| 25 | 25 | |
| 26 | +#include "libavutil/avassert.h" |
| 26 | 27 | #include "libavutil/common.h" |
| 27 | 28 | #include "libavutil/hwcontext_mediacodec.h" |
| 28 | 29 | #include "libavutil/mem.h" |
| @@ -30,6 +31,7 @@ · 1 unmodified line |
| 30 | 31 | #include "libavutil/pixfmt.h" |
| 31 | 32 | #include "libavutil/time.h" |
| 32 | 33 | #include "libavutil/timestamp.h" |
| 34 | +#include "libavutil/channel_layout.h" |
| 33 | 35 | |
| 34 | 36 | #include "avcodec.h" |
| 35 | 37 | #include "decode.h" |
| @@ -85,6 +87,107 @@ · 49 unmodified lines |
| 85 | 87 | #define OUTPUT_DEQUEUE_TIMEOUT_US 8000 |
| 86 | 88 | #define OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US 1000000 |
| 87 | 89 | |
| 90 | +enum { |
| 91 | + ENCODING_PCM_16BIT = 0x00000002, |
| 92 | + ENCODING_PCM_8BIT = 0x00000003, |
| 93 | + ENCODING_PCM_FLOAT = 0x00000004, |
| 94 | + ENCODING_PCM_24BIT_PACKED = 0x00000015, |
| 95 | + ENCODING_PCM_32BIT = 0x00000016, |
| 96 | +}; |
| 97 | + |
| 98 | +static const struct { |
| 99 | + |
| 100 | + int pcm_format; |
| 101 | + enum AVSampleFormat sample_format; |
| 102 | + |
| 103 | +} sample_formats[] = { |
| 104 | + |
| 105 | + { ENCODING_PCM_16BIT, AV_SAMPLE_FMT_S16 }, |
| 106 | + { ENCODING_PCM_8BIT, AV_SAMPLE_FMT_U8 }, |
| 107 | + { ENCODING_PCM_FLOAT, AV_SAMPLE_FMT_FLT }, |
| 108 | + { ENCODING_PCM_32BIT, AV_SAMPLE_FMT_S32 }, |
| 109 | + { 0 } |
| 110 | +}; |
| 111 | + |
| 112 | +static enum AVSampleFormat mcdec_map_pcm_format(AVCodecContext *avctx, |
| 113 | + MediaCodecDecContext *s, |
| 114 | + int pcm_format) |
| 115 | +{ |
| 116 | + enum AVSampleFormat ret = AV_SAMPLE_FMT_NONE; |
| 117 | + |
| 118 | + for (int i = 0; i < FF_ARRAY_ELEMS(sample_formats); i++) { |
| 119 | + if (sample_formats[i].pcm_format == pcm_format) { |
| 120 | + return sample_formats[i].sample_format; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + av_log(avctx, AV_LOG_ERROR, "Output sample format 0x%x (value=%d) is not supported\n", |
| 125 | + pcm_format, pcm_format); |
| 126 | + |
| 127 | + return ret; |
| 128 | +} |
| 129 | + |
| 130 | +enum |
| 131 | +{ |
| 132 | + CHANNEL_OUT_FRONT_LEFT = 0x4, |
| 133 | + CHANNEL_OUT_FRONT_RIGHT = 0x8, |
| 134 | + CHANNEL_OUT_FRONT_CENTER = 0x10, |
| 135 | + CHANNEL_OUT_LOW_FREQUENCY = 0x20, |
| 136 | + CHANNEL_OUT_BACK_LEFT = 0x40, |
| 137 | + CHANNEL_OUT_BACK_RIGHT = 0x80, |
| 138 | + CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x100, |
| 139 | + CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x200, |
| 140 | + CHANNEL_OUT_BACK_CENTER = 0x400, |
| 141 | + CHANNEL_OUT_SIDE_LEFT = 0x800, |
| 142 | + CHANNEL_OUT_SIDE_RIGHT = 0x1000, |
| 143 | + CHANNEL_OUT_TOP_CENTER = 0x2000, |
| 144 | + CHANNEL_OUT_TOP_FRONT_LEFT = 0x4000, |
| 145 | + CHANNEL_OUT_TOP_FRONT_CENTER = 0x8000, |
| 146 | + CHANNEL_OUT_TOP_FRONT_RIGHT = 0x10000, |
| 147 | + CHANNEL_OUT_TOP_BACK_LEFT = 0x20000, |
| 148 | + CHANNEL_OUT_TOP_BACK_CENTER = 0x40000, |
| 149 | + CHANNEL_OUT_TOP_BACK_RIGHT = 0x80000, |
| 150 | +}; |
| 151 | + |
| 152 | +static const struct { |
| 153 | + |
| 154 | + int mask; |
| 155 | + uint64_t layout; |
| 156 | + |
| 157 | +} channel_masks[] = { |
| 158 | + { CHANNEL_OUT_FRONT_LEFT, AV_CH_FRONT_LEFT }, |
| 159 | + { CHANNEL_OUT_FRONT_RIGHT, AV_CH_FRONT_RIGHT }, |
| 160 | + { CHANNEL_OUT_FRONT_CENTER, AV_CH_FRONT_CENTER }, |
| 161 | + { CHANNEL_OUT_LOW_FREQUENCY, AV_CH_LOW_FREQUENCY }, |
| 162 | + { CHANNEL_OUT_BACK_LEFT, AV_CH_BACK_LEFT }, |
| 163 | + { CHANNEL_OUT_BACK_RIGHT, AV_CH_BACK_RIGHT }, |
| 164 | + { CHANNEL_OUT_FRONT_LEFT_OF_CENTER, AV_CH_FRONT_LEFT_OF_CENTER }, |
| 165 | + { CHANNEL_OUT_FRONT_RIGHT_OF_CENTER, AV_CH_FRONT_RIGHT_OF_CENTER }, |
| 166 | + { CHANNEL_OUT_BACK_CENTER, AV_CH_BACK_CENTER }, |
| 167 | + { CHANNEL_OUT_SIDE_LEFT, AV_CH_SIDE_LEFT }, |
| 168 | + { CHANNEL_OUT_SIDE_RIGHT, AV_CH_SIDE_RIGHT }, |
| 169 | + { CHANNEL_OUT_TOP_CENTER, AV_CH_TOP_CENTER }, |
| 170 | + { CHANNEL_OUT_TOP_FRONT_LEFT, AV_CH_TOP_FRONT_LEFT }, |
| 171 | + { CHANNEL_OUT_TOP_FRONT_CENTER, AV_CH_TOP_FRONT_CENTER }, |
| 172 | + { CHANNEL_OUT_TOP_FRONT_RIGHT, AV_CH_TOP_FRONT_RIGHT }, |
| 173 | + { CHANNEL_OUT_TOP_BACK_LEFT, AV_CH_TOP_BACK_LEFT }, |
| 174 | + { CHANNEL_OUT_TOP_BACK_CENTER, AV_CH_TOP_BACK_CENTER }, |
| 175 | + { CHANNEL_OUT_TOP_BACK_RIGHT, AV_CH_TOP_BACK_RIGHT }, |
| 176 | +}; |
| 177 | + |
| 178 | +static uint64_t mcdec_map_channel_mask(AVCodecContext *avctx, |
| 179 | + int channel_mask) |
| 180 | +{ |
| 181 | + uint64_t channel_layout = 0; |
| 182 | + |
| 183 | + for (int i = 0; i < FF_ARRAY_ELEMS(channel_masks); i++) { |
| 184 | + if (channel_mask & channel_masks[i].mask) |
| 185 | + channel_layout |= channel_masks[i].layout; |
| 186 | + } |
| 187 | + |
| 188 | + return channel_layout; |
| 189 | +} |
| 190 | + |
| 88 | 191 | enum { |
| 89 | 192 | COLOR_FormatYUV420Planar = 0x13, |
| 90 | 193 | COLOR_FormatYUV420SemiPlanar = 0x15, |
| @@ -265,13 +368,79 @@ fail: · 174 unmodified lines |
| 265 | 368 | return ret; |
| 266 | 369 | } |
| 267 | 370 | |
| 268 | | −static int mediacodec_wrap_sw_buffer(AVCodecContext *avctx, |
| 269 | | − MediaCodecDecContext *s, |
| 270 | | − uint8_t *data, |
| 271 | | − size_t size, |
| 272 | | − ssize_t index, |
| 273 | | − FFAMediaCodecBufferInfo *info, |
| 274 | | − AVFrame *frame) |
| 371 | +static int mediacodec_wrap_sw_audio_buffer(AVCodecContext *avctx, |
| 372 | + MediaCodecDecContext *s, |
| 373 | + uint8_t *data, |
| 374 | + size_t size, |
| 375 | + ssize_t index, |
| 376 | + FFAMediaCodecBufferInfo *info, |
| 377 | + AVFrame *frame) |
| 378 | +{ |
| 379 | + int ret = 0; |
| 380 | + int status = 0; |
| 381 | + const int sample_size = av_get_bytes_per_sample(avctx->sample_fmt); |
| 382 | + if (!sample_size) { |
| 383 | + av_log(avctx, AV_LOG_ERROR, "Could not get bytes per sample\n"); |
| 384 | + ret = AVERROR(ENOSYS); |
| 385 | + goto done; |
| 386 | + } |
| 387 | + |
| 388 | + frame->format = avctx->sample_fmt; |
| 389 | + frame->sample_rate = avctx->sample_rate; |
| 390 | + frame->nb_samples = info->size / (sample_size * avctx->ch_layout.nb_channels); |
| 391 | + |
| 392 | + ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout); |
| 393 | + if (ret < 0) { |
| 394 | + av_log(avctx, AV_LOG_ERROR, "Could not copy channel layout\n"); |
| 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 | + */ |
| 401 | + ret = ff_get_buffer(avctx, frame, 0); |
| 402 | + if (ret < 0) { |
| 403 | + av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer\n"); |
| 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 */ |
| 411 | + if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) { |
| 412 | + frame->pts = av_rescale_q(info->presentationTimeUs, |
| 413 | + AV_TIME_BASE_Q, |
| 414 | + avctx->pkt_timebase); |
| 415 | + } else { |
| 416 | + frame->pts = info->presentationTimeUs; |
| 417 | + } |
| 418 | + frame->pkt_dts = AV_NOPTS_VALUE; |
| 419 | + |
| 420 | + av_log(avctx, AV_LOG_TRACE, |
| 421 | + "Frame: format=%d channels=%d sample_rate=%d nb_samples=%d", |
| 422 | + avctx->sample_fmt, avctx->ch_layout.nb_channels, avctx->sample_rate, frame->nb_samples); |
| 423 | + |
| 424 | + memcpy(frame->data[0], data, info->size); |
| 425 | + |
| 426 | + ret = 0; |
| 427 | +done: |
| 428 | + status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0); |
| 429 | + if (status < 0) { |
| 430 | + av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n"); |
| 431 | + ret = AVERROR_EXTERNAL; |
| 432 | + } |
| 433 | + |
| 434 | + return ret; |
| 435 | +} |
| 436 | + |
| 437 | +static int mediacodec_wrap_sw_video_buffer(AVCodecContext *avctx, |
| 438 | + MediaCodecDecContext *s, |
| 439 | + uint8_t *data, |
| 440 | + size_t size, |
| 441 | + ssize_t index, |
| 442 | + FFAMediaCodecBufferInfo *info, |
| 443 | + AVFrame *frame) |
| 275 | 444 | { |
| 276 | 445 | int ret = 0; |
| 277 | 446 | int status = 0; |
| @@ -343,6 +512,22 @@ done: · 65 unmodified lines |
| 343 | 512 | return ret; |
| 344 | 513 | } |
| 345 | 514 | |
| 515 | +static int mediacodec_wrap_sw_buffer(AVCodecContext *avctx, |
| 516 | + MediaCodecDecContext *s, |
| 517 | + uint8_t *data, |
| 518 | + size_t size, |
| 519 | + ssize_t index, |
| 520 | + FFAMediaCodecBufferInfo *info, |
| 521 | + AVFrame *frame) |
| 522 | +{ |
| 523 | + if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) |
| 524 | + return mediacodec_wrap_sw_audio_buffer(avctx, s, data, size, index, info, frame); |
| 525 | + else if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) |
| 526 | + return mediacodec_wrap_sw_video_buffer(avctx, s, data, size, index, info, frame); |
| 527 | + else |
| 528 | + av_assert0(0); |
| 529 | +} |
| 530 | + |
| 346 | 531 | #define AMEDIAFORMAT_GET_INT32(name, key, mandatory) do { \ |
| 347 | 532 | int32_t value = 0; \ |
| 348 | 533 | if (ff_AMediaFormat_getInt32(s->format, key, &value)) { \ |
| @@ -354,7 +539,7 @@ done: · 5 unmodified lines |
| 354 | 539 | } \ |
| 355 | 540 | } while (0) \ |
| 356 | 541 | |
| 357 | | −static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s) |
| 542 | +static int mediacodec_dec_parse_video_format(AVCodecContext *avctx, MediaCodecDecContext *s) |
| 358 | 543 | { |
| 359 | 544 | int ret = 0; |
| 360 | 545 | int width = 0; |
| @@ -463,6 +648,63 @@ fail: · 102 unmodified lines |
| 463 | 648 | return ret; |
| 464 | 649 | } |
| 465 | 650 | |
| 651 | +static int mediacodec_dec_parse_audio_format(AVCodecContext *avctx, MediaCodecDecContext *s) |
| 652 | +{ |
| 653 | + int ret = 0; |
| 654 | + int sample_rate = 0; |
| 655 | + int channel_count = 0; |
| 656 | + int channel_mask = 0; |
| 657 | + int pcm_encoding = 0; |
| 658 | + char *format = NULL; |
| 659 | + |
| 660 | + if (!s->format) { |
| 661 | + av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n"); |
| 662 | + return AVERROR(EINVAL); |
| 663 | + } |
| 664 | + |
| 665 | + format = ff_AMediaFormat_toString(s->format); |
| 666 | + if (!format) { |
| 667 | + return AVERROR_EXTERNAL; |
| 668 | + } |
| 669 | + av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format); |
| 670 | + |
| 671 | + /* Mandatory fields */ |
| 672 | + AMEDIAFORMAT_GET_INT32(channel_count, "channel-count", 1); |
| 673 | + AMEDIAFORMAT_GET_INT32(sample_rate, "sample-rate", 1); |
| 674 | + |
| 675 | + AMEDIAFORMAT_GET_INT32(pcm_encoding, "pcm-encoding", 0); |
| 676 | + if (pcm_encoding) |
| 677 | + avctx->sample_fmt = mcdec_map_pcm_format(avctx, s, pcm_encoding); |
| 678 | + else |
| 679 | + avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
| 680 | + |
| 681 | + avctx->sample_rate = sample_rate; |
| 682 | + |
| 683 | + AMEDIAFORMAT_GET_INT32(channel_mask, "channel-mask", 0); |
| 684 | + if (channel_mask) |
| 685 | + av_channel_layout_from_mask(&avctx->ch_layout, mcdec_map_channel_mask(avctx, channel_mask)); |
| 686 | + else |
| 687 | + av_channel_layout_default(&avctx->ch_layout, channel_count); |
| 688 | + |
| 689 | + av_log(avctx, AV_LOG_INFO, |
| 690 | + "Output parameters channel-count=%d channel-layout=%x sample-rate=%d\n", |
| 691 | + channel_count, channel_mask, sample_rate); |
| 692 | + |
| 693 | +fail: |
| 694 | + av_freep(&format); |
| 695 | + return ret; |
| 696 | +} |
| 697 | + |
| 698 | +static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s) |
| 699 | +{ |
| 700 | + if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) |
| 701 | + return mediacodec_dec_parse_audio_format(avctx, s); |
| 702 | + else if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) |
| 703 | + return mediacodec_dec_parse_video_format(avctx, s); |
| 704 | + else |
| 705 | + av_assert0(0); |
| 706 | +} |
| 707 | + |
| 466 | 708 | static int mediacodec_dec_flush_codec(AVCodecContext *avctx, MediaCodecDecContext *s) |
| 467 | 709 | { |
| 468 | 710 | FFAMediaCodec *codec = s->codec; |
| @@ -486,11 +728,9 @@ static int mediacodec_dec_flush_codec(AVCodecContext *avctx, MediaCodecDecContex · 17 unmodified lines |
| 486 | 728 | return 0; |
| 487 | 729 | } |
| 488 | 730 | |
| 489 | | −int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, |
| 490 | | − const char *mime, FFAMediaFormat *format) |
| 731 | +static int mediacodec_dec_get_video_codec(AVCodecContext *avctx, MediaCodecDecContext *s, |
| 732 | + const char *mime, FFAMediaFormat *format) |
| 491 | 733 | { |
| 492 | | − int ret = 0; |
| 493 | | − int status; |
| 494 | 734 | int profile; |
| 495 | 735 | |
| 496 | 736 | enum AVPixelFormat pix_fmt; |
| @@ -499,12 +739,6 @@ int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, · 2 unmodified lines |
| 499 | 739 | AV_PIX_FMT_NONE, |
| 500 | 740 | }; |
| 501 | 741 | |
| 502 | | − s->avctx = avctx; |
| 503 | | − atomic_init(&s->refcount, 1); |
| 504 | | − atomic_init(&s->hw_buffer_count, 0); |
| 505 | | − atomic_init(&s->serial, 1); |
| 506 | | − s->current_input_buffer = -1; |
| 507 | | − |
| 508 | 742 | pix_fmt = ff_get_format(avctx, pix_fmts); |
| 509 | 743 | if (pix_fmt == AV_PIX_FMT_MEDIACODEC) { |
| 510 | 744 | AVMediaCodecContext *user_ctx = avctx->hwaccel_context; |
| @@ -536,8 +770,7 @@ int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, · 25 unmodified lines |
| 536 | 770 | // getCodecNameByType() can fail due to missing JVM, while NDK |
| 537 | 771 | // mediacodec can be used without JVM. |
| 538 | 772 | if (!s->use_ndk_codec) { |
| 539 | | − ret = AVERROR_EXTERNAL; |
| 540 | | − goto fail; |
| 773 | + return AVERROR_EXTERNAL; |
| 541 | 774 | } |
| 542 | 775 | av_log(avctx, AV_LOG_INFO, "Failed to getCodecNameByType\n"); |
| 543 | 776 | } else { |
| @@ -556,10 +789,52 @@ int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, · 12 unmodified lines |
| 556 | 789 | } |
| 557 | 790 | if (!s->codec) { |
| 558 | 791 | av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name); |
| 559 | | − ret = AVERROR_EXTERNAL; |
| 560 | | − goto fail; |
| 792 | + return AVERROR_EXTERNAL; |
| 793 | + } |
| 794 | + |
| 795 | + return 0; |
| 796 | +} |
| 797 | + |
| 798 | +static int mediacodec_dec_get_audio_codec(AVCodecContext *avctx, MediaCodecDecContext *s, |
| 799 | + const char *mime, FFAMediaFormat *format) |
| 800 | +{ |
| 801 | + s->codec = ff_AMediaCodec_createDecoderByType(mime, s->use_ndk_codec); |
| 802 | + if (!s->codec) { |
| 803 | + av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for mime %s\n", mime); |
| 804 | + return AVERROR_EXTERNAL; |
| 805 | + } |
| 806 | + |
| 807 | + s->codec_name = ff_AMediaCodec_getName(s->codec); |
| 808 | + if (!s->codec_name) { |
| 809 | + s->codec_name = av_strdup(mime); |
| 810 | + if (!s->codec_name) |
| 811 | + return AVERROR(ENOMEM); |
| 561 | 812 | } |
| 562 | 813 | |
| 814 | + return 0; |
| 815 | +} |
| 816 | + |
| 817 | +int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, |
| 818 | + const char *mime, FFAMediaFormat *format) |
| 819 | +{ |
| 820 | + int ret; |
| 821 | + int status; |
| 822 | + |
| 823 | + s->avctx = avctx; |
| 824 | + atomic_init(&s->refcount, 1); |
| 825 | + atomic_init(&s->hw_buffer_count, 0); |
| 826 | + atomic_init(&s->serial, 1); |
| 827 | + s->current_input_buffer = -1; |
| 828 | + |
| 829 | + if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) |
| 830 | + ret = mediacodec_dec_get_audio_codec(avctx, s, mime, format); |
| 831 | + else if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) |
| 832 | + ret = mediacodec_dec_get_video_codec(avctx, s, mime, format); |
| 833 | + else |
| 834 | + av_assert0(0); |
| 835 | + if (ret < 0) |
| 836 | + goto fail; |
| 837 | + |
| 563 | 838 | status = ff_AMediaCodec_configure(s->codec, format, s->surface, NULL, 0); |
| 564 | 839 | if (status < 0) { |
| 565 | 840 | char *desc = ff_AMediaFormat_toString(format); |
| @@ -583,12 +858,14 @@ int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, · 17 unmodified lines |
| 583 | 858 | goto fail; |
| 584 | 859 | } |
| 585 | 860 | |
| 586 | | − s->format = ff_AMediaCodec_getOutputFormat(s->codec); |
| 587 | | − if (s->format) { |
| 588 | | − if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) { |
| 589 | | − av_log(avctx, AV_LOG_ERROR, |
| 590 | | − "Failed to configure context\n"); |
| 591 | | − goto fail; |
| 861 | + if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 862 | + s->format = ff_AMediaCodec_getOutputFormat(s->codec); |
| 863 | + if (s->format) { |
| 864 | + if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) { |
| 865 | + av_log(avctx, AV_LOG_ERROR, |
| 866 | + "Failed to configure context\n"); |
| 867 | + goto fail; |
| 868 | + } |
| 592 | 869 | } |
| 593 | 870 | } |
| 594 | 871 | |