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.

765 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/common.h"
  25. #include "libavutil/hwcontext_mediacodec.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_common.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. atomic_fetch_add(&s->refcount, 1);
  129. }
  130. static void ff_mediacodec_dec_unref(MediaCodecDecContext *s)
  131. {
  132. if (!s)
  133. return;
  134. if (atomic_fetch_sub(&s->refcount, 1) == 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 = atomic_load(&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. atomic_init(&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. if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
  244. frame->pts = av_rescale_q(info->presentationTimeUs,
  245. av_make_q(1, 1000000),
  246. avctx->pkt_timebase);
  247. } else {
  248. frame->pts = info->presentationTimeUs;
  249. }
  250. #if FF_API_PKT_PTS
  251. FF_DISABLE_DEPRECATION_WARNINGS
  252. frame->pkt_pts = frame->pts;
  253. FF_ENABLE_DEPRECATION_WARNINGS
  254. #endif
  255. frame->pkt_dts = AV_NOPTS_VALUE;
  256. av_log(avctx, AV_LOG_TRACE,
  257. "Frame: width=%d stride=%d height=%d slice-height=%d "
  258. "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s\n"
  259. "destination linesizes=%d,%d,%d\n" ,
  260. avctx->width, s->stride, avctx->height, s->slice_height,
  261. s->crop_top, s->crop_bottom, s->crop_left, s->crop_right, s->codec_name,
  262. frame->linesize[0], frame->linesize[1], frame->linesize[2]);
  263. switch (s->color_format) {
  264. case COLOR_FormatYUV420Planar:
  265. ff_mediacodec_sw_buffer_copy_yuv420_planar(avctx, s, data, size, info, frame);
  266. break;
  267. case COLOR_FormatYUV420SemiPlanar:
  268. case COLOR_QCOM_FormatYUV420SemiPlanar:
  269. case COLOR_QCOM_FormatYUV420SemiPlanar32m:
  270. ff_mediacodec_sw_buffer_copy_yuv420_semi_planar(avctx, s, data, size, info, frame);
  271. break;
  272. case COLOR_TI_FormatYUV420PackedSemiPlanar:
  273. case COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced:
  274. ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar(avctx, s, data, size, info, frame);
  275. break;
  276. case COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka:
  277. ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar_64x32Tile2m8ka(avctx, s, data, size, info, frame);
  278. break;
  279. default:
  280. av_log(avctx, AV_LOG_ERROR, "Unsupported color format 0x%x (value=%d)\n",
  281. s->color_format, s->color_format);
  282. ret = AVERROR(EINVAL);
  283. goto done;
  284. }
  285. ret = 0;
  286. done:
  287. status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
  288. if (status < 0) {
  289. av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
  290. ret = AVERROR_EXTERNAL;
  291. }
  292. return ret;
  293. }
  294. #define AMEDIAFORMAT_GET_INT32(name, key, mandatory) do { \
  295. int32_t value = 0; \
  296. if (ff_AMediaFormat_getInt32(s->format, key, &value)) { \
  297. (name) = value; \
  298. } else if (mandatory) { \
  299. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", key, format); \
  300. ret = AVERROR_EXTERNAL; \
  301. goto fail; \
  302. } \
  303. } while (0) \
  304. static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s)
  305. {
  306. int ret = 0;
  307. int width = 0;
  308. int height = 0;
  309. char *format = NULL;
  310. if (!s->format) {
  311. av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n");
  312. return AVERROR(EINVAL);
  313. }
  314. format = ff_AMediaFormat_toString(s->format);
  315. if (!format) {
  316. return AVERROR_EXTERNAL;
  317. }
  318. av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format);
  319. /* Mandatory fields */
  320. AMEDIAFORMAT_GET_INT32(s->width, "width", 1);
  321. AMEDIAFORMAT_GET_INT32(s->height, "height", 1);
  322. AMEDIAFORMAT_GET_INT32(s->stride, "stride", 1);
  323. s->stride = s->stride > 0 ? s->stride : s->width;
  324. AMEDIAFORMAT_GET_INT32(s->slice_height, "slice-height", 1);
  325. s->slice_height = s->slice_height > 0 ? s->slice_height : s->height;
  326. if (strstr(s->codec_name, "OMX.Nvidia.")) {
  327. s->slice_height = FFALIGN(s->height, 16);
  328. } else if (strstr(s->codec_name, "OMX.SEC.avc.dec")) {
  329. s->slice_height = avctx->height;
  330. s->stride = avctx->width;
  331. }
  332. AMEDIAFORMAT_GET_INT32(s->color_format, "color-format", 1);
  333. avctx->pix_fmt = mcdec_map_color_format(avctx, s, s->color_format);
  334. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  335. av_log(avctx, AV_LOG_ERROR, "Output color format is not supported\n");
  336. ret = AVERROR(EINVAL);
  337. goto fail;
  338. }
  339. /* Optional fields */
  340. AMEDIAFORMAT_GET_INT32(s->crop_top, "crop-top", 0);
  341. AMEDIAFORMAT_GET_INT32(s->crop_bottom, "crop-bottom", 0);
  342. AMEDIAFORMAT_GET_INT32(s->crop_left, "crop-left", 0);
  343. AMEDIAFORMAT_GET_INT32(s->crop_right, "crop-right", 0);
  344. width = s->crop_right + 1 - s->crop_left;
  345. height = s->crop_bottom + 1 - s->crop_top;
  346. av_log(avctx, AV_LOG_INFO,
  347. "Output crop parameters top=%d bottom=%d left=%d right=%d, "
  348. "resulting dimensions width=%d height=%d\n",
  349. s->crop_top, s->crop_bottom, s->crop_left, s->crop_right,
  350. width, height);
  351. av_freep(&format);
  352. return ff_set_dimensions(avctx, width, height);
  353. fail:
  354. av_freep(&format);
  355. return ret;
  356. }
  357. static int mediacodec_dec_flush_codec(AVCodecContext *avctx, MediaCodecDecContext *s)
  358. {
  359. FFAMediaCodec *codec = s->codec;
  360. int status;
  361. s->output_buffer_count = 0;
  362. s->draining = 0;
  363. s->flushing = 0;
  364. s->eos = 0;
  365. status = ff_AMediaCodec_flush(codec);
  366. if (status < 0) {
  367. av_log(avctx, AV_LOG_ERROR, "Failed to flush codec\n");
  368. return AVERROR_EXTERNAL;
  369. }
  370. return 0;
  371. }
  372. int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s,
  373. const char *mime, FFAMediaFormat *format)
  374. {
  375. int ret = 0;
  376. int status;
  377. int profile;
  378. enum AVPixelFormat pix_fmt;
  379. static const enum AVPixelFormat pix_fmts[] = {
  380. AV_PIX_FMT_MEDIACODEC,
  381. AV_PIX_FMT_NONE,
  382. };
  383. atomic_init(&s->refcount, 1);
  384. pix_fmt = ff_get_format(avctx, pix_fmts);
  385. if (pix_fmt == AV_PIX_FMT_MEDIACODEC) {
  386. AVMediaCodecContext *user_ctx = avctx->hwaccel_context;
  387. if (avctx->hw_device_ctx) {
  388. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)(avctx->hw_device_ctx->data);
  389. if (device_ctx->type == AV_HWDEVICE_TYPE_MEDIACODEC) {
  390. if (device_ctx->hwctx) {
  391. AVMediaCodecDeviceContext *mediacodec_ctx = (AVMediaCodecDeviceContext *)device_ctx->hwctx;
  392. s->surface = ff_mediacodec_surface_ref(mediacodec_ctx->surface, avctx);
  393. av_log(avctx, AV_LOG_INFO, "Using surface %p\n", s->surface);
  394. }
  395. }
  396. }
  397. if (!s->surface && user_ctx && user_ctx->surface) {
  398. s->surface = ff_mediacodec_surface_ref(user_ctx->surface, avctx);
  399. av_log(avctx, AV_LOG_INFO, "Using surface %p\n", s->surface);
  400. }
  401. }
  402. profile = ff_AMediaCodecProfile_getProfileFromAVCodecContext(avctx);
  403. if (profile < 0) {
  404. av_log(avctx, AV_LOG_WARNING, "Unsupported or unknown profile\n");
  405. }
  406. s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, profile, 0, avctx);
  407. if (!s->codec_name) {
  408. ret = AVERROR_EXTERNAL;
  409. goto fail;
  410. }
  411. av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name);
  412. s->codec = ff_AMediaCodec_createCodecByName(s->codec_name);
  413. if (!s->codec) {
  414. av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name);
  415. ret = AVERROR_EXTERNAL;
  416. goto fail;
  417. }
  418. status = ff_AMediaCodec_configure(s->codec, format, s->surface, NULL, 0);
  419. if (status < 0) {
  420. char *desc = ff_AMediaFormat_toString(format);
  421. av_log(avctx, AV_LOG_ERROR,
  422. "Failed to configure codec (status = %d) with format %s\n",
  423. status, desc);
  424. av_freep(&desc);
  425. ret = AVERROR_EXTERNAL;
  426. goto fail;
  427. }
  428. status = ff_AMediaCodec_start(s->codec);
  429. if (status < 0) {
  430. char *desc = ff_AMediaFormat_toString(format);
  431. av_log(avctx, AV_LOG_ERROR,
  432. "Failed to start codec (status = %d) with format %s\n",
  433. status, desc);
  434. av_freep(&desc);
  435. ret = AVERROR_EXTERNAL;
  436. goto fail;
  437. }
  438. s->format = ff_AMediaCodec_getOutputFormat(s->codec);
  439. if (s->format) {
  440. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  441. av_log(avctx, AV_LOG_ERROR,
  442. "Failed to configure context\n");
  443. goto fail;
  444. }
  445. }
  446. av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", s->codec);
  447. return 0;
  448. fail:
  449. av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", s->codec);
  450. ff_mediacodec_dec_close(avctx, s);
  451. return ret;
  452. }
  453. int ff_mediacodec_dec_send(AVCodecContext *avctx, MediaCodecDecContext *s,
  454. AVPacket *pkt)
  455. {
  456. int offset = 0;
  457. int need_draining = 0;
  458. uint8_t *data;
  459. ssize_t index;
  460. size_t size;
  461. FFAMediaCodec *codec = s->codec;
  462. int status;
  463. int64_t input_dequeue_timeout_us = INPUT_DEQUEUE_TIMEOUT_US;
  464. if (s->flushing) {
  465. av_log(avctx, AV_LOG_ERROR, "Decoder is flushing and cannot accept new buffer "
  466. "until all output buffers have been released\n");
  467. return AVERROR_EXTERNAL;
  468. }
  469. if (pkt->size == 0) {
  470. need_draining = 1;
  471. }
  472. if (s->draining && s->eos) {
  473. return AVERROR_EOF;
  474. }
  475. while (offset < pkt->size || (need_draining && !s->draining)) {
  476. index = ff_AMediaCodec_dequeueInputBuffer(codec, input_dequeue_timeout_us);
  477. if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  478. av_log(avctx, AV_LOG_TRACE, "Failed to dequeue input buffer, try again later..\n");
  479. break;
  480. }
  481. if (index < 0) {
  482. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue input buffer (status=%zd)\n", index);
  483. return AVERROR_EXTERNAL;
  484. }
  485. data = ff_AMediaCodec_getInputBuffer(codec, index, &size);
  486. if (!data) {
  487. av_log(avctx, AV_LOG_ERROR, "Failed to get input buffer\n");
  488. return AVERROR_EXTERNAL;
  489. }
  490. if (need_draining) {
  491. int64_t pts = pkt->pts;
  492. uint32_t flags = ff_AMediaCodec_getBufferFlagEndOfStream(codec);
  493. if (s->surface) {
  494. pts = av_rescale_q(pts, avctx->pkt_timebase, av_make_q(1, 1000000));
  495. }
  496. av_log(avctx, AV_LOG_DEBUG, "Sending End Of Stream signal\n");
  497. status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, 0, pts, flags);
  498. if (status < 0) {
  499. av_log(avctx, AV_LOG_ERROR, "Failed to queue input empty buffer (status = %d)\n", status);
  500. return AVERROR_EXTERNAL;
  501. }
  502. av_log(avctx, AV_LOG_TRACE, "Queued input buffer %zd"
  503. " size=%zd ts=%" PRIi64 "\n", index, size, pts);
  504. s->draining = 1;
  505. break;
  506. } else {
  507. int64_t pts = pkt->pts;
  508. size = FFMIN(pkt->size - offset, size);
  509. memcpy(data, pkt->data + offset, size);
  510. offset += size;
  511. if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
  512. pts = av_rescale_q(pts, avctx->pkt_timebase, av_make_q(1, 1000000));
  513. }
  514. status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pts, 0);
  515. if (status < 0) {
  516. av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status);
  517. return AVERROR_EXTERNAL;
  518. }
  519. }
  520. }
  521. if (offset == 0)
  522. return AVERROR(EAGAIN);
  523. return offset;
  524. }
  525. int ff_mediacodec_dec_receive(AVCodecContext *avctx, MediaCodecDecContext *s,
  526. AVFrame *frame, bool wait)
  527. {
  528. int ret;
  529. uint8_t *data;
  530. ssize_t index;
  531. size_t size;
  532. FFAMediaCodec *codec = s->codec;
  533. FFAMediaCodecBufferInfo info = { 0 };
  534. int status;
  535. int64_t output_dequeue_timeout_us = OUTPUT_DEQUEUE_TIMEOUT_US;
  536. if (s->draining && s->eos) {
  537. return AVERROR_EOF;
  538. }
  539. if (s->draining) {
  540. /* If the codec is flushing or need to be flushed, block for a fair
  541. * amount of time to ensure we got a frame */
  542. output_dequeue_timeout_us = OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US;
  543. } else if (s->output_buffer_count == 0 || !wait) {
  544. /* If the codec hasn't produced any frames, do not block so we
  545. * can push data to it as fast as possible, and get the first
  546. * frame */
  547. output_dequeue_timeout_us = 0;
  548. }
  549. index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, output_dequeue_timeout_us);
  550. if (index >= 0) {
  551. av_log(avctx, AV_LOG_TRACE, "Got output buffer %zd"
  552. " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
  553. " flags=%" PRIu32 "\n", index, info.offset, info.size,
  554. info.presentationTimeUs, info.flags);
  555. if (info.flags & ff_AMediaCodec_getBufferFlagEndOfStream(codec)) {
  556. s->eos = 1;
  557. }
  558. if (info.size) {
  559. if (s->surface) {
  560. if ((ret = mediacodec_wrap_hw_buffer(avctx, s, index, &info, frame)) < 0) {
  561. av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
  562. return ret;
  563. }
  564. } else {
  565. data = ff_AMediaCodec_getOutputBuffer(codec, index, &size);
  566. if (!data) {
  567. av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n");
  568. return AVERROR_EXTERNAL;
  569. }
  570. if ((ret = mediacodec_wrap_sw_buffer(avctx, s, data, size, index, &info, frame)) < 0) {
  571. av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
  572. return ret;
  573. }
  574. }
  575. s->output_buffer_count++;
  576. return 0;
  577. } else {
  578. status = ff_AMediaCodec_releaseOutputBuffer(codec, index, 0);
  579. if (status < 0) {
  580. av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
  581. }
  582. }
  583. } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) {
  584. char *format = NULL;
  585. if (s->format) {
  586. status = ff_AMediaFormat_delete(s->format);
  587. if (status < 0) {
  588. av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format);
  589. }
  590. }
  591. s->format = ff_AMediaCodec_getOutputFormat(codec);
  592. if (!s->format) {
  593. av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n");
  594. return AVERROR_EXTERNAL;
  595. }
  596. format = ff_AMediaFormat_toString(s->format);
  597. if (!format) {
  598. return AVERROR_EXTERNAL;
  599. }
  600. av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format);
  601. av_freep(&format);
  602. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  603. return ret;
  604. }
  605. } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) {
  606. ff_AMediaCodec_cleanOutputBuffers(codec);
  607. } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  608. if (s->draining) {
  609. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer within %" PRIi64 "ms "
  610. "while draining remaining frames, output will probably lack frames\n",
  611. output_dequeue_timeout_us / 1000);
  612. } else {
  613. av_log(avctx, AV_LOG_DEBUG, "No output buffer available, try again later\n");
  614. }
  615. } else {
  616. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index);
  617. return AVERROR_EXTERNAL;
  618. }
  619. return AVERROR(EAGAIN);
  620. }
  621. int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s)
  622. {
  623. if (!s->surface || atomic_load(&s->refcount) == 1) {
  624. int ret;
  625. /* No frames (holding a reference to the codec) are retained by the
  626. * user, thus we can flush the codec and returns accordingly */
  627. if ((ret = mediacodec_dec_flush_codec(avctx, s)) < 0) {
  628. return ret;
  629. }
  630. return 1;
  631. }
  632. s->flushing = 1;
  633. return 0;
  634. }
  635. int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s)
  636. {
  637. ff_mediacodec_dec_unref(s);
  638. return 0;
  639. }
  640. int ff_mediacodec_dec_is_flushing(AVCodecContext *avctx, MediaCodecDecContext *s)
  641. {
  642. return s->flushing;
  643. }