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.

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