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.

769 lines
25KB

  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. #include <string.h>
  23. #include <sys/types.h>
  24. #include "libavutil/atomic.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/mem.h"
  27. #include "libavutil/log.h"
  28. #include "libavutil/pixfmt.h"
  29. #include "libavutil/time.h"
  30. #include "libavutil/timestamp.h"
  31. #include "avcodec.h"
  32. #include "internal.h"
  33. #include "mediacodec.h"
  34. #include "mediacodec_surface.h"
  35. #include "mediacodec_sw_buffer.h"
  36. #include "mediacodec_wrapper.h"
  37. #include "mediacodecdec.h"
  38. /**
  39. * OMX.k3.video.decoder.avc, OMX.NVIDIA.* OMX.SEC.avc.dec and OMX.google
  40. * codec workarounds used in various place are taken from the Gstreamer
  41. * project.
  42. *
  43. * Gstreamer references:
  44. * https://cgit.freedesktop.org/gstreamer/gst-plugins-bad/tree/sys/androidmedia/
  45. *
  46. * Gstreamer copyright notice:
  47. *
  48. * Copyright (C) 2012, Collabora Ltd.
  49. * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
  50. *
  51. * Copyright (C) 2012, Rafaël Carré <funman@videolanorg>
  52. *
  53. * Copyright (C) 2015, Sebastian Dröge <sebastian@centricular.com>
  54. *
  55. * Copyright (C) 2014-2015, Collabora Ltd.
  56. * Author: Matthieu Bouron <matthieu.bouron@gcollabora.com>
  57. *
  58. * Copyright (C) 2015, Edward Hervey
  59. * Author: Edward Hervey <bilboed@gmail.com>
  60. *
  61. * Copyright (C) 2015, Matthew Waters <matthew@centricular.com>
  62. *
  63. * This library is free software; you can redistribute it and/or
  64. * modify it under the terms of the GNU Lesser General Public
  65. * License as published by the Free Software Foundation
  66. * version 2.1 of the License.
  67. *
  68. * This library is distributed in the hope that it will be useful,
  69. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  70. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  71. * Lesser General Public License for more details.
  72. *
  73. * You should have received a copy of the GNU Lesser General Public
  74. * License along with this library; if not, write to the Free Software
  75. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  76. *
  77. */
  78. #define INPUT_DEQUEUE_TIMEOUT_US 8000
  79. #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
  80. #define OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US 1000000
  81. enum {
  82. COLOR_FormatYUV420Planar = 0x13,
  83. COLOR_FormatYUV420SemiPlanar = 0x15,
  84. COLOR_FormatYCbYCr = 0x19,
  85. COLOR_FormatAndroidOpaque = 0x7F000789,
  86. COLOR_QCOM_FormatYUV420SemiPlanar = 0x7fa30c00,
  87. COLOR_QCOM_FormatYUV420SemiPlanar32m = 0x7fa30c04,
  88. COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka = 0x7fa30c03,
  89. COLOR_TI_FormatYUV420PackedSemiPlanar = 0x7f000100,
  90. COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced = 0x7f000001,
  91. };
  92. static const struct {
  93. int color_format;
  94. enum AVPixelFormat pix_fmt;
  95. } color_formats[] = {
  96. { COLOR_FormatYUV420Planar, AV_PIX_FMT_YUV420P },
  97. { COLOR_FormatYUV420SemiPlanar, AV_PIX_FMT_NV12 },
  98. { COLOR_QCOM_FormatYUV420SemiPlanar, AV_PIX_FMT_NV12 },
  99. { COLOR_QCOM_FormatYUV420SemiPlanar32m, AV_PIX_FMT_NV12 },
  100. { COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka, AV_PIX_FMT_NV12 },
  101. { COLOR_TI_FormatYUV420PackedSemiPlanar, AV_PIX_FMT_NV12 },
  102. { COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced, AV_PIX_FMT_NV12 },
  103. { 0 }
  104. };
  105. static enum AVPixelFormat mcdec_map_color_format(AVCodecContext *avctx,
  106. MediaCodecDecContext *s,
  107. int color_format)
  108. {
  109. int i;
  110. enum AVPixelFormat ret = AV_PIX_FMT_NONE;
  111. if (s->surface) {
  112. return AV_PIX_FMT_MEDIACODEC;
  113. }
  114. if (!strcmp(s->codec_name, "OMX.k3.video.decoder.avc") && color_format == COLOR_FormatYCbYCr) {
  115. s->color_format = color_format = COLOR_TI_FormatYUV420PackedSemiPlanar;
  116. }
  117. for (i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) {
  118. if (color_formats[i].color_format == color_format) {
  119. return color_formats[i].pix_fmt;
  120. }
  121. }
  122. av_log(avctx, AV_LOG_ERROR, "Output color format 0x%x (value=%d) is not supported\n",
  123. color_format, color_format);
  124. return ret;
  125. }
  126. static void ff_mediacodec_dec_ref(MediaCodecDecContext *s)
  127. {
  128. avpriv_atomic_int_add_and_fetch(&s->refcount, 1);
  129. }
  130. static void ff_mediacodec_dec_unref(MediaCodecDecContext *s)
  131. {
  132. if (!s)
  133. return;
  134. if (!avpriv_atomic_int_add_and_fetch(&s->refcount, -1)) {
  135. if (s->codec) {
  136. ff_AMediaCodec_delete(s->codec);
  137. s->codec = NULL;
  138. }
  139. if (s->format) {
  140. ff_AMediaFormat_delete(s->format);
  141. s->format = NULL;
  142. }
  143. if (s->surface) {
  144. ff_mediacodec_surface_unref(s->surface, NULL);
  145. s->surface = NULL;
  146. }
  147. av_freep(&s->codec_name);
  148. av_freep(&s);
  149. }
  150. }
  151. static void mediacodec_buffer_release(void *opaque, uint8_t *data)
  152. {
  153. AVMediaCodecBuffer *buffer = opaque;
  154. MediaCodecDecContext *ctx = buffer->ctx;
  155. int released = avpriv_atomic_int_get(&buffer->released);
  156. if (!released) {
  157. ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, 0);
  158. }
  159. ff_mediacodec_dec_unref(ctx);
  160. av_freep(&buffer);
  161. }
  162. static int mediacodec_wrap_hw_buffer(AVCodecContext *avctx,
  163. MediaCodecDecContext *s,
  164. ssize_t index,
  165. FFAMediaCodecBufferInfo *info,
  166. AVFrame *frame)
  167. {
  168. int ret = 0;
  169. int status = 0;
  170. AVMediaCodecBuffer *buffer = NULL;
  171. frame->buf[0] = NULL;
  172. frame->width = avctx->width;
  173. frame->height = avctx->height;
  174. frame->format = avctx->pix_fmt;
  175. if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
  176. frame->pts = av_rescale_q(info->presentationTimeUs,
  177. av_make_q(1, 1000000),
  178. avctx->pkt_timebase);
  179. } else {
  180. frame->pts = info->presentationTimeUs;
  181. }
  182. #if FF_API_PKT_PTS
  183. FF_DISABLE_DEPRECATION_WARNINGS
  184. frame->pkt_pts = frame->pts;
  185. FF_ENABLE_DEPRECATION_WARNINGS
  186. #endif
  187. frame->pkt_dts = AV_NOPTS_VALUE;
  188. buffer = av_mallocz(sizeof(AVMediaCodecBuffer));
  189. if (!buffer) {
  190. ret = AVERROR(ENOMEM);
  191. goto fail;
  192. }
  193. buffer->released = 0;
  194. frame->buf[0] = av_buffer_create(NULL,
  195. 0,
  196. mediacodec_buffer_release,
  197. buffer,
  198. AV_BUFFER_FLAG_READONLY);
  199. if (!frame->buf[0]) {
  200. ret = AVERROR(ENOMEM);
  201. goto fail;
  202. }
  203. buffer->ctx = s;
  204. ff_mediacodec_dec_ref(s);
  205. buffer->index = index;
  206. buffer->pts = info->presentationTimeUs;
  207. frame->data[3] = (uint8_t *)buffer;
  208. return 0;
  209. fail:
  210. av_freep(buffer);
  211. av_buffer_unref(&frame->buf[0]);
  212. status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
  213. if (status < 0) {
  214. av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
  215. ret = AVERROR_EXTERNAL;
  216. }
  217. return ret;
  218. }
  219. static int mediacodec_wrap_sw_buffer(AVCodecContext *avctx,
  220. MediaCodecDecContext *s,
  221. uint8_t *data,
  222. size_t size,
  223. ssize_t index,
  224. FFAMediaCodecBufferInfo *info,
  225. AVFrame *frame)
  226. {
  227. int ret = 0;
  228. int status = 0;
  229. frame->width = avctx->width;
  230. frame->height = avctx->height;
  231. frame->format = avctx->pix_fmt;
  232. /* MediaCodec buffers needs to be copied to our own refcounted buffers
  233. * because the flush command invalidates all input and output buffers.
  234. */
  235. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  236. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer\n");
  237. goto done;
  238. }
  239. /* Override frame->pkt_pts as ff_get_buffer will override its value based
  240. * on the last avpacket received which is not in sync with the frame:
  241. * * N avpackets can be pushed before 1 frame is actually returned
  242. * * 0-sized avpackets are pushed to flush remaining frames at EOS */
  243. frame->pts = info->presentationTimeUs;
  244. #if FF_API_PKT_PTS
  245. FF_DISABLE_DEPRECATION_WARNINGS
  246. frame->pkt_pts = info->presentationTimeUs;
  247. FF_ENABLE_DEPRECATION_WARNINGS
  248. #endif
  249. frame->pkt_dts = AV_NOPTS_VALUE;
  250. av_log(avctx, AV_LOG_DEBUG,
  251. "Frame: width=%d stride=%d height=%d slice-height=%d "
  252. "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s\n"
  253. "destination linesizes=%d,%d,%d\n" ,
  254. avctx->width, s->stride, avctx->height, s->slice_height,
  255. s->crop_top, s->crop_bottom, s->crop_left, s->crop_right, s->codec_name,
  256. frame->linesize[0], frame->linesize[1], frame->linesize[2]);
  257. switch (s->color_format) {
  258. case COLOR_FormatYUV420Planar:
  259. ff_mediacodec_sw_buffer_copy_yuv420_planar(avctx, s, data, size, info, frame);
  260. break;
  261. case COLOR_FormatYUV420SemiPlanar:
  262. case COLOR_QCOM_FormatYUV420SemiPlanar:
  263. case COLOR_QCOM_FormatYUV420SemiPlanar32m:
  264. ff_mediacodec_sw_buffer_copy_yuv420_semi_planar(avctx, s, data, size, info, frame);
  265. break;
  266. case COLOR_TI_FormatYUV420PackedSemiPlanar:
  267. case COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced:
  268. ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar(avctx, s, data, size, info, frame);
  269. break;
  270. case COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka:
  271. ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar_64x32Tile2m8ka(avctx, s, data, size, info, frame);
  272. break;
  273. default:
  274. av_log(avctx, AV_LOG_ERROR, "Unsupported color format 0x%x (value=%d)\n",
  275. s->color_format, s->color_format);
  276. ret = AVERROR(EINVAL);
  277. goto done;
  278. }
  279. ret = 0;
  280. done:
  281. status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
  282. if (status < 0) {
  283. av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
  284. ret = AVERROR_EXTERNAL;
  285. }
  286. return ret;
  287. }
  288. static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s)
  289. {
  290. int width = 0;
  291. int height = 0;
  292. int32_t value = 0;
  293. char *format = NULL;
  294. if (!s->format) {
  295. av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n");
  296. return AVERROR(EINVAL);
  297. }
  298. format = ff_AMediaFormat_toString(s->format);
  299. if (!format) {
  300. return AVERROR_EXTERNAL;
  301. }
  302. av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format);
  303. av_freep(&format);
  304. /* Mandatory fields */
  305. if (!ff_AMediaFormat_getInt32(s->format, "width", &value)) {
  306. format = ff_AMediaFormat_toString(s->format);
  307. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "width", format);
  308. av_freep(&format);
  309. return AVERROR_EXTERNAL;
  310. }
  311. s->width = value;
  312. if (!ff_AMediaFormat_getInt32(s->format, "height", &value)) {
  313. format = ff_AMediaFormat_toString(s->format);
  314. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "height", format);
  315. av_freep(&format);
  316. return AVERROR_EXTERNAL;
  317. }
  318. s->height = value;
  319. if (!ff_AMediaFormat_getInt32(s->format, "stride", &value)) {
  320. format = ff_AMediaFormat_toString(s->format);
  321. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "stride", format);
  322. av_freep(&format);
  323. return AVERROR_EXTERNAL;
  324. }
  325. s->stride = value > 0 ? value : s->width;
  326. if (!ff_AMediaFormat_getInt32(s->format, "slice-height", &value)) {
  327. format = ff_AMediaFormat_toString(s->format);
  328. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "slice-height", format);
  329. av_freep(&format);
  330. return AVERROR_EXTERNAL;
  331. }
  332. s->slice_height = value > 0 ? value : s->height;
  333. if (strstr(s->codec_name, "OMX.Nvidia.")) {
  334. s->slice_height = FFALIGN(s->height, 16);
  335. } else if (strstr(s->codec_name, "OMX.SEC.avc.dec")) {
  336. s->slice_height = avctx->height;
  337. s->stride = avctx->width;
  338. }
  339. if (!ff_AMediaFormat_getInt32(s->format, "color-format", &value)) {
  340. format = ff_AMediaFormat_toString(s->format);
  341. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "color-format", format);
  342. av_freep(&format);
  343. return AVERROR_EXTERNAL;
  344. }
  345. s->color_format = value;
  346. s->pix_fmt = avctx->pix_fmt = mcdec_map_color_format(avctx, s, value);
  347. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  348. av_log(avctx, AV_LOG_ERROR, "Output color format is not supported\n");
  349. return AVERROR(EINVAL);
  350. }
  351. /* Optional fields */
  352. if (ff_AMediaFormat_getInt32(s->format, "crop-top", &value))
  353. s->crop_top = value;
  354. if (ff_AMediaFormat_getInt32(s->format, "crop-bottom", &value))
  355. s->crop_bottom = value;
  356. if (ff_AMediaFormat_getInt32(s->format, "crop-left", &value))
  357. s->crop_left = value;
  358. if (ff_AMediaFormat_getInt32(s->format, "crop-right", &value))
  359. s->crop_right = value;
  360. width = s->crop_right + 1 - s->crop_left;
  361. height = s->crop_bottom + 1 - s->crop_top;
  362. av_log(avctx, AV_LOG_INFO,
  363. "Output crop parameters top=%d bottom=%d left=%d right=%d, "
  364. "resulting dimensions width=%d height=%d\n",
  365. s->crop_top, s->crop_bottom, s->crop_left, s->crop_right,
  366. width, height);
  367. return ff_set_dimensions(avctx, width, height);
  368. }
  369. static int mediacodec_dec_flush_codec(AVCodecContext *avctx, MediaCodecDecContext *s)
  370. {
  371. FFAMediaCodec *codec = s->codec;
  372. int status;
  373. s->output_buffer_count = 0;
  374. s->draining = 0;
  375. s->flushing = 0;
  376. s->eos = 0;
  377. status = ff_AMediaCodec_flush(codec);
  378. if (status < 0) {
  379. av_log(avctx, AV_LOG_ERROR, "Failed to flush codec\n");
  380. return AVERROR_EXTERNAL;
  381. }
  382. return 0;
  383. }
  384. int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s,
  385. const char *mime, FFAMediaFormat *format)
  386. {
  387. int ret = 0;
  388. int status;
  389. int profile;
  390. enum AVPixelFormat pix_fmt;
  391. static const enum AVPixelFormat pix_fmts[] = {
  392. AV_PIX_FMT_MEDIACODEC,
  393. AV_PIX_FMT_NONE,
  394. };
  395. s->refcount = 1;
  396. pix_fmt = ff_get_format(avctx, pix_fmts);
  397. if (pix_fmt == AV_PIX_FMT_MEDIACODEC) {
  398. AVMediaCodecContext *user_ctx = avctx->hwaccel_context;
  399. if (user_ctx && user_ctx->surface) {
  400. s->surface = ff_mediacodec_surface_ref(user_ctx->surface, avctx);
  401. av_log(avctx, AV_LOG_INFO, "Using surface %p\n", s->surface);
  402. }
  403. }
  404. profile = ff_AMediaCodecProfile_getProfileFromAVCodecContext(avctx);
  405. if (profile < 0) {
  406. av_log(avctx, AV_LOG_WARNING, "Unsupported or unknown profile");
  407. }
  408. s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, profile, 0, avctx);
  409. if (!s->codec_name) {
  410. ret = AVERROR_EXTERNAL;
  411. goto fail;
  412. }
  413. av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name);
  414. s->codec = ff_AMediaCodec_createCodecByName(s->codec_name);
  415. if (!s->codec) {
  416. av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name);
  417. ret = AVERROR_EXTERNAL;
  418. goto fail;
  419. }
  420. status = ff_AMediaCodec_configure(s->codec, format, s->surface, NULL, 0);
  421. if (status < 0) {
  422. char *desc = ff_AMediaFormat_toString(format);
  423. av_log(avctx, AV_LOG_ERROR,
  424. "Failed to configure codec (status = %d) with format %s\n",
  425. status, desc);
  426. av_freep(&desc);
  427. ret = AVERROR_EXTERNAL;
  428. goto fail;
  429. }
  430. status = ff_AMediaCodec_start(s->codec);
  431. if (status < 0) {
  432. char *desc = ff_AMediaFormat_toString(format);
  433. av_log(avctx, AV_LOG_ERROR,
  434. "Failed to start codec (status = %d) with format %s\n",
  435. status, desc);
  436. av_freep(&desc);
  437. ret = AVERROR_EXTERNAL;
  438. goto fail;
  439. }
  440. s->format = ff_AMediaCodec_getOutputFormat(s->codec);
  441. if (s->format) {
  442. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  443. av_log(avctx, AV_LOG_ERROR,
  444. "Failed to configure context\n");
  445. goto fail;
  446. }
  447. }
  448. av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", s->codec);
  449. return 0;
  450. fail:
  451. av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", s->codec);
  452. ff_mediacodec_dec_close(avctx, s);
  453. return ret;
  454. }
  455. int ff_mediacodec_dec_decode(AVCodecContext *avctx, MediaCodecDecContext *s,
  456. AVFrame *frame, int *got_frame,
  457. AVPacket *pkt)
  458. {
  459. int ret;
  460. int offset = 0;
  461. int need_draining = 0;
  462. uint8_t *data;
  463. ssize_t index;
  464. size_t size;
  465. FFAMediaCodec *codec = s->codec;
  466. FFAMediaCodecBufferInfo info = { 0 };
  467. int status;
  468. int64_t input_dequeue_timeout_us = INPUT_DEQUEUE_TIMEOUT_US;
  469. int64_t output_dequeue_timeout_us = OUTPUT_DEQUEUE_TIMEOUT_US;
  470. if (s->flushing) {
  471. av_log(avctx, AV_LOG_ERROR, "Decoder is flushing and cannot accept new buffer "
  472. "until all output buffers have been released\n");
  473. return AVERROR_EXTERNAL;
  474. }
  475. if (pkt->size == 0) {
  476. need_draining = 1;
  477. }
  478. if (s->draining && s->eos) {
  479. return 0;
  480. }
  481. while (offset < pkt->size || (need_draining && !s->draining)) {
  482. index = ff_AMediaCodec_dequeueInputBuffer(codec, input_dequeue_timeout_us);
  483. if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  484. break;
  485. }
  486. if (index < 0) {
  487. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue input buffer (status=%zd)\n", index);
  488. return AVERROR_EXTERNAL;
  489. }
  490. data = ff_AMediaCodec_getInputBuffer(codec, index, &size);
  491. if (!data) {
  492. av_log(avctx, AV_LOG_ERROR, "Failed to get input buffer\n");
  493. return AVERROR_EXTERNAL;
  494. }
  495. if (need_draining) {
  496. int64_t pts = pkt->pts;
  497. uint32_t flags = ff_AMediaCodec_getBufferFlagEndOfStream(codec);
  498. if (s->surface) {
  499. pts = av_rescale_q(pts, avctx->pkt_timebase, av_make_q(1, 1000000));
  500. }
  501. av_log(avctx, AV_LOG_DEBUG, "Sending End Of Stream signal\n");
  502. status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, 0, pts, flags);
  503. if (status < 0) {
  504. av_log(avctx, AV_LOG_ERROR, "Failed to queue input empty buffer (status = %d)\n", status);
  505. return AVERROR_EXTERNAL;
  506. }
  507. s->draining = 1;
  508. break;
  509. } else {
  510. int64_t pts = pkt->pts;
  511. size = FFMIN(pkt->size - offset, size);
  512. memcpy(data, pkt->data + offset, size);
  513. offset += size;
  514. if (s->surface && avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
  515. pts = av_rescale_q(pts, avctx->pkt_timebase, av_make_q(1, 1000000));
  516. }
  517. status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pts, 0);
  518. if (status < 0) {
  519. av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status);
  520. return AVERROR_EXTERNAL;
  521. }
  522. }
  523. }
  524. if (need_draining || s->draining) {
  525. /* If the codec is flushing or need to be flushed, block for a fair
  526. * amount of time to ensure we got a frame */
  527. output_dequeue_timeout_us = OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US;
  528. } else if (s->output_buffer_count == 0) {
  529. /* If the codec hasn't produced any frames, do not block so we
  530. * can push data to it as fast as possible, and get the first
  531. * frame */
  532. output_dequeue_timeout_us = 0;
  533. }
  534. index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, output_dequeue_timeout_us);
  535. if (index >= 0) {
  536. int ret;
  537. av_log(avctx, AV_LOG_DEBUG, "Got output buffer %zd"
  538. " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
  539. " flags=%" PRIu32 "\n", index, info.offset, info.size,
  540. info.presentationTimeUs, info.flags);
  541. if (info.flags & ff_AMediaCodec_getBufferFlagEndOfStream(codec)) {
  542. s->eos = 1;
  543. }
  544. if (info.size) {
  545. if (s->surface) {
  546. if ((ret = mediacodec_wrap_hw_buffer(avctx, s, index, &info, frame)) < 0) {
  547. av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
  548. return ret;
  549. }
  550. } else {
  551. data = ff_AMediaCodec_getOutputBuffer(codec, index, &size);
  552. if (!data) {
  553. av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n");
  554. return AVERROR_EXTERNAL;
  555. }
  556. if ((ret = mediacodec_wrap_sw_buffer(avctx, s, data, size, index, &info, frame)) < 0) {
  557. av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
  558. return ret;
  559. }
  560. }
  561. *got_frame = 1;
  562. s->output_buffer_count++;
  563. } else {
  564. status = ff_AMediaCodec_releaseOutputBuffer(codec, index, 0);
  565. if (status < 0) {
  566. av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
  567. }
  568. }
  569. } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) {
  570. char *format = NULL;
  571. if (s->format) {
  572. status = ff_AMediaFormat_delete(s->format);
  573. if (status < 0) {
  574. av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format);
  575. }
  576. }
  577. s->format = ff_AMediaCodec_getOutputFormat(codec);
  578. if (!s->format) {
  579. av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n");
  580. return AVERROR_EXTERNAL;
  581. }
  582. format = ff_AMediaFormat_toString(s->format);
  583. if (!format) {
  584. return AVERROR_EXTERNAL;
  585. }
  586. av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format);
  587. av_freep(&format);
  588. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  589. return ret;
  590. }
  591. } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) {
  592. ff_AMediaCodec_cleanOutputBuffers(codec);
  593. } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  594. if (s->draining) {
  595. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer within %" PRIi64 "ms "
  596. "while draining remaining frames, output will probably lack frames\n",
  597. output_dequeue_timeout_us / 1000);
  598. } else {
  599. av_log(avctx, AV_LOG_DEBUG, "No output buffer available, try again later\n");
  600. }
  601. } else {
  602. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index);
  603. return AVERROR_EXTERNAL;
  604. }
  605. return offset;
  606. }
  607. int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s)
  608. {
  609. if (!s->surface || avpriv_atomic_int_get(&s->refcount) == 1) {
  610. int ret;
  611. /* No frames (holding a reference to the codec) are retained by the
  612. * user, thus we can flush the codec and returns accordingly */
  613. if ((ret = mediacodec_dec_flush_codec(avctx, s)) < 0) {
  614. return ret;
  615. }
  616. return 1;
  617. }
  618. s->flushing = 1;
  619. return 0;
  620. }
  621. int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s)
  622. {
  623. ff_mediacodec_dec_unref(s);
  624. return 0;
  625. }
  626. int ff_mediacodec_dec_is_flushing(AVCodecContext *avctx, MediaCodecDecContext *s)
  627. {
  628. return s->flushing;
  629. }
  630. AVHWAccel ff_h264_mediacodec_hwaccel = {
  631. .name = "mediacodec",
  632. .type = AVMEDIA_TYPE_VIDEO,
  633. .id = AV_CODEC_ID_H264,
  634. .pix_fmt = AV_PIX_FMT_MEDIACODEC,
  635. };
  636. AVHWAccel ff_hevc_mediacodec_hwaccel = {
  637. .name = "mediacodec",
  638. .type = AVMEDIA_TYPE_VIDEO,
  639. .id = AV_CODEC_ID_HEVC,
  640. .pix_fmt = AV_PIX_FMT_MEDIACODEC,
  641. };