You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

556 lines
16KB

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