mediacodecdec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * Android MediaCodec MPEG-2 / H.264 / H.265 / MPEG-4 / VP8 / VP9 decoders
  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. #include "config_components.h"
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/pixfmt.h"
  30. #include "libavutil/internal.h"
  31. #include "avcodec.h"
  32. #include "codec_internal.h"
  33. #include "decode.h"
  34. #include "h264_parse.h"
  35. #include "h264_ps.h"
  36. #include "hevc_parse.h"
  37. #include "hwconfig.h"
  38. #include "internal.h"
  39. #include "jni.h"
  40. #include "mediacodec_wrapper.h"
  41. #include "mediacodecdec_common.h"
  42. typedef struct MediaCodecH264DecContext {
  43. AVClass *avclass;
  44. MediaCodecDecContext *ctx;
  45. AVPacket buffered_pkt;
  46. int delay_flush;
  47. int amlogic_mpeg2_api23_workaround;
  48. int use_ndk_codec;
  49. } MediaCodecH264DecContext;
  50. static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
  51. {
  52. MediaCodecH264DecContext *s = avctx->priv_data;
  53. ff_mediacodec_dec_close(avctx, s->ctx);
  54. s->ctx = NULL;
  55. av_packet_unref(&s->buffered_pkt);
  56. return 0;
  57. }
  58. #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
  59. static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
  60. {
  61. int i;
  62. int ret = 0;
  63. uint8_t *p = NULL;
  64. static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
  65. if (!out || !out_size) {
  66. return AVERROR(EINVAL);
  67. }
  68. p = av_malloc(sizeof(nalu_header) + src_size);
  69. if (!p) {
  70. return AVERROR(ENOMEM);
  71. }
  72. *out = p;
  73. *out_size = sizeof(nalu_header) + src_size;
  74. memcpy(p, nalu_header, sizeof(nalu_header));
  75. memcpy(p + sizeof(nalu_header), src, src_size);
  76. /* Escape 0x00, 0x00, 0x0{0-3} pattern */
  77. for (i = 4; i < *out_size; i++) {
  78. if (i < *out_size - 3 &&
  79. p[i + 0] == 0 &&
  80. p[i + 1] == 0 &&
  81. p[i + 2] <= 3) {
  82. uint8_t *new;
  83. *out_size += 1;
  84. new = av_realloc(*out, *out_size);
  85. if (!new) {
  86. ret = AVERROR(ENOMEM);
  87. goto done;
  88. }
  89. *out = p = new;
  90. i = i + 2;
  91. memmove(p + i + 1, p + i, *out_size - (i + 1));
  92. p[i] = 0x03;
  93. }
  94. }
  95. done:
  96. if (ret < 0) {
  97. av_freep(out);
  98. *out_size = 0;
  99. }
  100. return ret;
  101. }
  102. #endif
  103. #if CONFIG_H264_MEDIACODEC_DECODER
  104. static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  105. {
  106. int i;
  107. int ret;
  108. H264ParamSets ps;
  109. const PPS *pps = NULL;
  110. const SPS *sps = NULL;
  111. int is_avc = 0;
  112. int nal_length_size = 0;
  113. memset(&ps, 0, sizeof(ps));
  114. ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
  115. &ps, &is_avc, &nal_length_size, 0, avctx);
  116. if (ret < 0) {
  117. goto done;
  118. }
  119. for (i = 0; i < MAX_PPS_COUNT; i++) {
  120. if (ps.pps_list[i]) {
  121. pps = (const PPS*)ps.pps_list[i]->data;
  122. break;
  123. }
  124. }
  125. if (pps) {
  126. if (ps.sps_list[pps->sps_id]) {
  127. sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
  128. }
  129. }
  130. if (pps && sps) {
  131. uint8_t *data = NULL;
  132. int data_size = 0;
  133. avctx->profile = ff_h264_get_profile(sps);
  134. avctx->level = sps->level_idc;
  135. if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
  136. goto done;
  137. }
  138. ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
  139. av_freep(&data);
  140. if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
  141. goto done;
  142. }
  143. ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
  144. av_freep(&data);
  145. } else {
  146. const int warn = is_avc && (avctx->codec_tag == MKTAG('a','v','c','1') ||
  147. avctx->codec_tag == MKTAG('a','v','c','2'));
  148. av_log(avctx, warn ? AV_LOG_WARNING : AV_LOG_DEBUG,
  149. "Could not extract PPS/SPS from extradata\n");
  150. ret = 0;
  151. }
  152. done:
  153. ff_h264_ps_uninit(&ps);
  154. return ret;
  155. }
  156. #endif
  157. #if CONFIG_HEVC_MEDIACODEC_DECODER
  158. static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  159. {
  160. int i;
  161. int ret;
  162. HEVCParamSets ps;
  163. HEVCSEI sei;
  164. const HEVCVPS *vps = NULL;
  165. const HEVCPPS *pps = NULL;
  166. const HEVCSPS *sps = NULL;
  167. int is_nalff = 0;
  168. int nal_length_size = 0;
  169. uint8_t *vps_data = NULL;
  170. uint8_t *sps_data = NULL;
  171. uint8_t *pps_data = NULL;
  172. int vps_data_size = 0;
  173. int sps_data_size = 0;
  174. int pps_data_size = 0;
  175. memset(&ps, 0, sizeof(ps));
  176. memset(&sei, 0, sizeof(sei));
  177. ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
  178. &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
  179. if (ret < 0) {
  180. goto done;
  181. }
  182. for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
  183. if (ps.vps_list[i]) {
  184. vps = (const HEVCVPS*)ps.vps_list[i]->data;
  185. break;
  186. }
  187. }
  188. for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
  189. if (ps.pps_list[i]) {
  190. pps = (const HEVCPPS*)ps.pps_list[i]->data;
  191. break;
  192. }
  193. }
  194. if (pps) {
  195. if (ps.sps_list[pps->sps_id]) {
  196. sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
  197. }
  198. }
  199. if (vps && pps && sps) {
  200. uint8_t *data;
  201. int data_size;
  202. avctx->profile = sps->ptl.general_ptl.profile_idc;
  203. avctx->level = sps->ptl.general_ptl.level_idc;
  204. if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
  205. (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
  206. (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
  207. goto done;
  208. }
  209. data_size = vps_data_size + sps_data_size + pps_data_size;
  210. data = av_mallocz(data_size);
  211. if (!data) {
  212. ret = AVERROR(ENOMEM);
  213. goto done;
  214. }
  215. memcpy(data , vps_data, vps_data_size);
  216. memcpy(data + vps_data_size , sps_data, sps_data_size);
  217. memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
  218. ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
  219. av_freep(&data);
  220. } else {
  221. const int warn = is_nalff && avctx->codec_tag == MKTAG('h','v','c','1');
  222. av_log(avctx, warn ? AV_LOG_WARNING : AV_LOG_DEBUG,
  223. "Could not extract VPS/PPS/SPS from extradata\n");
  224. ret = 0;
  225. }
  226. done:
  227. ff_hevc_ps_uninit(&ps);
  228. av_freep(&vps_data);
  229. av_freep(&sps_data);
  230. av_freep(&pps_data);
  231. return ret;
  232. }
  233. #endif
  234. #if CONFIG_MPEG2_MEDIACODEC_DECODER || \
  235. CONFIG_MPEG4_MEDIACODEC_DECODER || \
  236. CONFIG_VP8_MEDIACODEC_DECODER || \
  237. CONFIG_VP9_MEDIACODEC_DECODER
  238. static int common_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  239. {
  240. int ret = 0;
  241. if (avctx->extradata) {
  242. ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
  243. }
  244. return ret;
  245. }
  246. #endif
  247. static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
  248. {
  249. int ret;
  250. int sdk_int;
  251. const char *codec_mime = NULL;
  252. FFAMediaFormat *format = NULL;
  253. MediaCodecH264DecContext *s = avctx->priv_data;
  254. if (s->use_ndk_codec < 0)
  255. s->use_ndk_codec = !av_jni_get_java_vm(avctx);
  256. format = ff_AMediaFormat_new(s->use_ndk_codec);
  257. if (!format) {
  258. av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
  259. ret = AVERROR_EXTERNAL;
  260. goto done;
  261. }
  262. switch (avctx->codec_id) {
  263. #if CONFIG_H264_MEDIACODEC_DECODER
  264. case AV_CODEC_ID_H264:
  265. codec_mime = "video/avc";
  266. ret = h264_set_extradata(avctx, format);
  267. if (ret < 0)
  268. goto done;
  269. break;
  270. #endif
  271. #if CONFIG_HEVC_MEDIACODEC_DECODER
  272. case AV_CODEC_ID_HEVC:
  273. codec_mime = "video/hevc";
  274. ret = hevc_set_extradata(avctx, format);
  275. if (ret < 0)
  276. goto done;
  277. break;
  278. #endif
  279. #if CONFIG_MPEG2_MEDIACODEC_DECODER
  280. case AV_CODEC_ID_MPEG2VIDEO:
  281. codec_mime = "video/mpeg2";
  282. ret = common_set_extradata(avctx, format);
  283. if (ret < 0)
  284. goto done;
  285. break;
  286. #endif
  287. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  288. case AV_CODEC_ID_MPEG4:
  289. codec_mime = "video/mp4v-es",
  290. ret = common_set_extradata(avctx, format);
  291. if (ret < 0)
  292. goto done;
  293. break;
  294. #endif
  295. #if CONFIG_VP8_MEDIACODEC_DECODER
  296. case AV_CODEC_ID_VP8:
  297. codec_mime = "video/x-vnd.on2.vp8";
  298. ret = common_set_extradata(avctx, format);
  299. if (ret < 0)
  300. goto done;
  301. break;
  302. #endif
  303. #if CONFIG_VP9_MEDIACODEC_DECODER
  304. case AV_CODEC_ID_VP9:
  305. codec_mime = "video/x-vnd.on2.vp9";
  306. ret = common_set_extradata(avctx, format);
  307. if (ret < 0)
  308. goto done;
  309. break;
  310. #endif
  311. default:
  312. av_assert0(0);
  313. }
  314. ff_AMediaFormat_setString(format, "mime", codec_mime);
  315. ff_AMediaFormat_setInt32(format, "width", avctx->width);
  316. ff_AMediaFormat_setInt32(format, "height", avctx->height);
  317. s->ctx = av_mallocz(sizeof(*s->ctx));
  318. if (!s->ctx) {
  319. av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
  320. ret = AVERROR(ENOMEM);
  321. goto done;
  322. }
  323. s->ctx->delay_flush = s->delay_flush;
  324. s->ctx->use_ndk_codec = s->use_ndk_codec;
  325. if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
  326. s->ctx = NULL;
  327. goto done;
  328. }
  329. av_log(avctx, AV_LOG_INFO,
  330. "MediaCodec started successfully: codec = %s, ret = %d\n",
  331. s->ctx->codec_name, ret);
  332. sdk_int = ff_Build_SDK_INT(avctx);
  333. if (sdk_int <= 23 &&
  334. strcmp(s->ctx->codec_name, "OMX.amlogic.mpeg2.decoder.awesome") == 0) {
  335. av_log(avctx, AV_LOG_INFO, "Enabling workaround for %s on API=%d\n",
  336. s->ctx->codec_name, sdk_int);
  337. s->amlogic_mpeg2_api23_workaround = 1;
  338. }
  339. done:
  340. if (format) {
  341. ff_AMediaFormat_delete(format);
  342. }
  343. if (ret < 0) {
  344. mediacodec_decode_close(avctx);
  345. }
  346. return ret;
  347. }
  348. static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  349. {
  350. MediaCodecH264DecContext *s = avctx->priv_data;
  351. int ret;
  352. ssize_t index;
  353. /* In delay_flush mode, wait until the user has released or rendered
  354. all retained frames. */
  355. if (s->delay_flush && ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
  356. if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
  357. return AVERROR(EAGAIN);
  358. }
  359. }
  360. /* poll for new frame */
  361. ret = ff_mediacodec_dec_receive(avctx, s->ctx, frame, false);
  362. if (ret != AVERROR(EAGAIN))
  363. return ret;
  364. /* feed decoder */
  365. while (1) {
  366. if (s->ctx->current_input_buffer < 0) {
  367. /* poll for input space */
  368. index = ff_AMediaCodec_dequeueInputBuffer(s->ctx->codec, 0);
  369. if (index < 0) {
  370. /* no space, block for an output frame to appear */
  371. ret = ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
  372. /* Try again if both input port and output port return EAGAIN.
  373. * If no data is consumed and no frame in output, it can make
  374. * both avcodec_send_packet() and avcodec_receive_frame()
  375. * return EAGAIN, which violate the design.
  376. */
  377. if (ff_AMediaCodec_infoTryAgainLater(s->ctx->codec, index) &&
  378. ret == AVERROR(EAGAIN))
  379. continue;
  380. return ret;
  381. }
  382. s->ctx->current_input_buffer = index;
  383. }
  384. /* try to flush any buffered packet data */
  385. if (s->buffered_pkt.size > 0) {
  386. ret = ff_mediacodec_dec_send(avctx, s->ctx, &s->buffered_pkt, false);
  387. if (ret >= 0) {
  388. s->buffered_pkt.size -= ret;
  389. s->buffered_pkt.data += ret;
  390. if (s->buffered_pkt.size <= 0) {
  391. av_packet_unref(&s->buffered_pkt);
  392. } else {
  393. av_log(avctx, AV_LOG_WARNING,
  394. "could not send entire packet in single input buffer (%d < %d)\n",
  395. ret, s->buffered_pkt.size+ret);
  396. }
  397. } else if (ret < 0 && ret != AVERROR(EAGAIN)) {
  398. return ret;
  399. }
  400. if (s->amlogic_mpeg2_api23_workaround && s->buffered_pkt.size <= 0) {
  401. /* fallthrough to fetch next packet regardless of input buffer space */
  402. } else {
  403. /* poll for space again */
  404. continue;
  405. }
  406. }
  407. /* fetch new packet or eof */
  408. ret = ff_decode_get_packet(avctx, &s->buffered_pkt);
  409. if (ret == AVERROR_EOF) {
  410. AVPacket null_pkt = { 0 };
  411. ret = ff_mediacodec_dec_send(avctx, s->ctx, &null_pkt, true);
  412. if (ret < 0)
  413. return ret;
  414. return ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
  415. } else if (ret == AVERROR(EAGAIN) && s->ctx->current_input_buffer < 0) {
  416. return ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
  417. } else if (ret < 0) {
  418. return ret;
  419. }
  420. }
  421. return AVERROR(EAGAIN);
  422. }
  423. static void mediacodec_decode_flush(AVCodecContext *avctx)
  424. {
  425. MediaCodecH264DecContext *s = avctx->priv_data;
  426. av_packet_unref(&s->buffered_pkt);
  427. ff_mediacodec_dec_flush(avctx, s->ctx);
  428. }
  429. static const AVCodecHWConfigInternal *const mediacodec_hw_configs[] = {
  430. &(const AVCodecHWConfigInternal) {
  431. .public = {
  432. .pix_fmt = AV_PIX_FMT_MEDIACODEC,
  433. .methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
  434. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
  435. .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
  436. },
  437. .hwaccel = NULL,
  438. },
  439. NULL
  440. };
  441. #define OFFSET(x) offsetof(MediaCodecH264DecContext, x)
  442. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  443. static const AVOption ff_mediacodec_vdec_options[] = {
  444. { "delay_flush", "Delay flush until hw output buffers are returned to the decoder",
  445. OFFSET(delay_flush), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VD },
  446. { "ndk_codec", "Use MediaCodec from NDK",
  447. OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VD },
  448. { NULL }
  449. };
  450. #define DECLARE_MEDIACODEC_VCLASS(short_name) \
  451. static const AVClass ff_##short_name##_mediacodec_dec_class = { \
  452. .class_name = #short_name "_mediacodec", \
  453. .item_name = av_default_item_name, \
  454. .option = ff_mediacodec_vdec_options, \
  455. .version = LIBAVUTIL_VERSION_INT, \
  456. };
  457. #define DECLARE_MEDIACODEC_VDEC(short_name, full_name, codec_id, bsf) \
  458. DECLARE_MEDIACODEC_VCLASS(short_name) \
  459. const FFCodec ff_ ## short_name ## _mediacodec_decoder = { \
  460. .p.name = #short_name "_mediacodec", \
  461. CODEC_LONG_NAME(full_name " Android MediaCodec decoder"), \
  462. .p.type = AVMEDIA_TYPE_VIDEO, \
  463. .p.id = codec_id, \
  464. .p.priv_class = &ff_##short_name##_mediacodec_dec_class, \
  465. .priv_data_size = sizeof(MediaCodecH264DecContext), \
  466. .init = mediacodec_decode_init, \
  467. FF_CODEC_RECEIVE_FRAME_CB(mediacodec_receive_frame), \
  468. .flush = mediacodec_decode_flush, \
  469. .close = mediacodec_decode_close, \
  470. .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
  471. .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE | \
  472. FF_CODEC_CAP_SETS_PKT_DTS, \
  473. .bsfs = bsf, \
  474. .hw_configs = mediacodec_hw_configs, \
  475. .p.wrapper_name = "mediacodec", \
  476. }; \
  477. #if CONFIG_H264_MEDIACODEC_DECODER
  478. DECLARE_MEDIACODEC_VDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb")
  479. #endif
  480. #if CONFIG_HEVC_MEDIACODEC_DECODER
  481. DECLARE_MEDIACODEC_VDEC(hevc, "H.265", AV_CODEC_ID_HEVC, "hevc_mp4toannexb")
  482. #endif
  483. #if CONFIG_MPEG2_MEDIACODEC_DECODER
  484. DECLARE_MEDIACODEC_VDEC(mpeg2, "MPEG-2", AV_CODEC_ID_MPEG2VIDEO, NULL)
  485. #endif
  486. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  487. DECLARE_MEDIACODEC_VDEC(mpeg4, "MPEG-4", AV_CODEC_ID_MPEG4, NULL)
  488. #endif
  489. #if CONFIG_VP8_MEDIACODEC_DECODER
  490. DECLARE_MEDIACODEC_VDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL)
  491. #endif
  492. #if CONFIG_VP9_MEDIACODEC_DECODER
  493. DECLARE_MEDIACODEC_VDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL)
  494. #endif