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.

770 lines
26KB

  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 && (ctx->delay_flush || buffer->serial == atomic_load(&ctx->serial))) {
  157. ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, 0);
  158. }
  159. if (ctx->delay_flush)
  160. ff_mediacodec_dec_unref(ctx);
  161. av_freep(&buffer);
  162. }
  163. static int mediacodec_wrap_hw_buffer(AVCodecContext *avctx,
  164. MediaCodecDecContext *s,
  165. ssize_t index,
  166. FFAMediaCodecBufferInfo *info,
  167. AVFrame *frame)
  168. {
  169. int ret = 0;
  170. int status = 0;
  171. AVMediaCodecBuffer *buffer = NULL;
  172. frame->buf[0] = NULL;
  173. frame->width = avctx->width;
  174. frame->height = avctx->height;
  175. frame->format = avctx->pix_fmt;
  176. if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
  177. frame->pts = av_rescale_q(info->presentationTimeUs,
  178. av_make_q(1, 1000000),
  179. avctx->pkt_timebase);
  180. } else {
  181. frame->pts = info->presentationTimeUs;
  182. }
  183. #if FF_API_PKT_PTS
  184. FF_DISABLE_DEPRECATION_WARNINGS
  185. frame->pkt_pts = frame->pts;
  186. FF_ENABLE_DEPRECATION_WARNINGS
  187. #endif
  188. frame->pkt_dts = AV_NOPTS_VALUE;
  189. buffer = av_mallocz(sizeof(AVMediaCodecBuffer));
  190. if (!buffer) {
  191. ret = AVERROR(ENOMEM);
  192. goto fail;
  193. }
  194. atomic_init(&buffer->released, 0);
  195. frame->buf[0] = av_buffer_create(NULL,
  196. 0,
  197. mediacodec_buffer_release,
  198. buffer,
  199. AV_BUFFER_FLAG_READONLY);
  200. if (!frame->buf[0]) {
  201. ret = AVERROR(ENOMEM);
  202. goto fail;
  203. }
  204. buffer->ctx = s;
  205. buffer->serial = atomic_load(&s->serial);
  206. if (s->delay_flush)
  207. ff_mediacodec_dec_ref(s);
  208. buffer->index = index;
  209. buffer->pts = info->presentationTimeUs;
  210. frame->data[3] = (uint8_t *)buffer;
  211. return 0;
  212. fail:
  213. av_freep(buffer);
  214. av_buffer_unref(&frame->buf[0]);
  215. status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
  216. if (status < 0) {
  217. av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
  218. ret = AVERROR_EXTERNAL;
  219. }
  220. return ret;
  221. }
  222. static int mediacodec_wrap_sw_buffer(AVCodecContext *avctx,
  223. MediaCodecDecContext *s,
  224. uint8_t *data,
  225. size_t size,
  226. ssize_t index,
  227. FFAMediaCodecBufferInfo *info,
  228. AVFrame *frame)
  229. {
  230. int ret = 0;
  231. int status = 0;
  232. frame->width = avctx->width;
  233. frame->height = avctx->height;
  234. frame->format = avctx->pix_fmt;
  235. /* MediaCodec buffers needs to be copied to our own refcounted buffers
  236. * because the flush command invalidates all input and output buffers.
  237. */
  238. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  239. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer\n");
  240. goto done;
  241. }
  242. /* Override frame->pkt_pts as ff_get_buffer will override its value based
  243. * on the last avpacket received which is not in sync with the frame:
  244. * * N avpackets can be pushed before 1 frame is actually returned
  245. * * 0-sized avpackets are pushed to flush remaining frames at EOS */
  246. if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
  247. frame->pts = av_rescale_q(info->presentationTimeUs,
  248. av_make_q(1, 1000000),
  249. avctx->pkt_timebase);
  250. } else {
  251. frame->pts = info->presentationTimeUs;
  252. }
  253. #if FF_API_PKT_PTS
  254. FF_DISABLE_DEPRECATION_WARNINGS
  255. frame->pkt_pts = frame->pts;
  256. FF_ENABLE_DEPRECATION_WARNINGS
  257. #endif
  258. frame->pkt_dts = AV_NOPTS_VALUE;
  259. av_log(avctx, AV_LOG_TRACE,
  260. "Frame: width=%d stride=%d height=%d slice-height=%d "
  261. "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s\n"
  262. "destination linesizes=%d,%d,%d\n" ,
  263. avctx->width, s->stride, avctx->height, s->slice_height,
  264. s->crop_top, s->crop_bottom, s->crop_left, s->crop_right, s->codec_name,
  265. frame->linesize[0], frame->linesize[1], frame->linesize[2]);
  266. switch (s->color_format) {
  267. case COLOR_FormatYUV420Planar:
  268. ff_mediacodec_sw_buffer_copy_yuv420_planar(avctx, s, data, size, info, frame);
  269. break;
  270. case COLOR_FormatYUV420SemiPlanar:
  271. case COLOR_QCOM_FormatYUV420SemiPlanar:
  272. case COLOR_QCOM_FormatYUV420SemiPlanar32m:
  273. ff_mediacodec_sw_buffer_copy_yuv420_semi_planar(avctx, s, data, size, info, frame);
  274. break;
  275. case COLOR_TI_FormatYUV420PackedSemiPlanar:
  276. case COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced:
  277. ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar(avctx, s, data, size, info, frame);
  278. break;
  279. case COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka:
  280. ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar_64x32Tile2m8ka(avctx, s, data, size, info, frame);
  281. break;
  282. default:
  283. av_log(avctx, AV_LOG_ERROR, "Unsupported color format 0x%x (value=%d)\n",
  284. s->color_format, s->color_format);
  285. ret = AVERROR(EINVAL);
  286. goto done;
  287. }
  288. ret = 0;
  289. done:
  290. status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
  291. if (status < 0) {
  292. av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
  293. ret = AVERROR_EXTERNAL;
  294. }
  295. return ret;
  296. }
  297. #define AMEDIAFORMAT_GET_INT32(name, key, mandatory) do { \
  298. int32_t value = 0; \
  299. if (ff_AMediaFormat_getInt32(s->format, key, &value)) { \
  300. (name) = value; \
  301. } else if (mandatory) { \
  302. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", key, format); \
  303. ret = AVERROR_EXTERNAL; \
  304. goto fail; \
  305. } \
  306. } while (0) \
  307. static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s)
  308. {
  309. int ret = 0;
  310. int width = 0;
  311. int height = 0;
  312. char *format = NULL;
  313. if (!s->format) {
  314. av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n");
  315. return AVERROR(EINVAL);
  316. }
  317. format = ff_AMediaFormat_toString(s->format);
  318. if (!format) {
  319. return AVERROR_EXTERNAL;
  320. }
  321. av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format);
  322. /* Mandatory fields */
  323. AMEDIAFORMAT_GET_INT32(s->width, "width", 1);
  324. AMEDIAFORMAT_GET_INT32(s->height, "height", 1);
  325. AMEDIAFORMAT_GET_INT32(s->stride, "stride", 1);
  326. s->stride = s->stride > 0 ? s->stride : s->width;
  327. AMEDIAFORMAT_GET_INT32(s->slice_height, "slice-height", 1);
  328. s->slice_height = s->slice_height > 0 ? s->slice_height : s->height;
  329. if (strstr(s->codec_name, "OMX.Nvidia.")) {
  330. s->slice_height = FFALIGN(s->height, 16);
  331. } else if (strstr(s->codec_name, "OMX.SEC.avc.dec")) {
  332. s->slice_height = avctx->height;
  333. s->stride = avctx->width;
  334. }
  335. AMEDIAFORMAT_GET_INT32(s->color_format, "color-format", 1);
  336. avctx->pix_fmt = mcdec_map_color_format(avctx, s, s->color_format);
  337. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  338. av_log(avctx, AV_LOG_ERROR, "Output color format is not supported\n");
  339. ret = AVERROR(EINVAL);
  340. goto fail;
  341. }
  342. /* Optional fields */
  343. AMEDIAFORMAT_GET_INT32(s->crop_top, "crop-top", 0);
  344. AMEDIAFORMAT_GET_INT32(s->crop_bottom, "crop-bottom", 0);
  345. AMEDIAFORMAT_GET_INT32(s->crop_left, "crop-left", 0);
  346. AMEDIAFORMAT_GET_INT32(s->crop_right, "crop-right", 0);
  347. width = s->crop_right + 1 - s->crop_left;
  348. height = s->crop_bottom + 1 - s->crop_top;
  349. av_log(avctx, AV_LOG_INFO,
  350. "Output crop parameters top=%d bottom=%d left=%d right=%d, "
  351. "resulting dimensions width=%d height=%d\n",
  352. s->crop_top, s->crop_bottom, s->crop_left, s->crop_right,
  353. width, height);
  354. av_freep(&format);
  355. return ff_set_dimensions(avctx, width, height);
  356. fail:
  357. av_freep(&format);
  358. return ret;
  359. }
  360. static int mediacodec_dec_flush_codec(AVCodecContext *avctx, MediaCodecDecContext *s)
  361. {
  362. FFAMediaCodec *codec = s->codec;
  363. int status;
  364. s->output_buffer_count = 0;
  365. s->draining = 0;
  366. s->flushing = 0;
  367. s->eos = 0;
  368. atomic_fetch_add(&s->serial, 1);
  369. status = ff_AMediaCodec_flush(codec);
  370. if (status < 0) {
  371. av_log(avctx, AV_LOG_ERROR, "Failed to flush codec\n");
  372. return AVERROR_EXTERNAL;
  373. }
  374. return 0;
  375. }
  376. int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s,
  377. const char *mime, FFAMediaFormat *format)
  378. {
  379. int ret = 0;
  380. int status;
  381. int profile;
  382. enum AVPixelFormat pix_fmt;
  383. static const enum AVPixelFormat pix_fmts[] = {
  384. AV_PIX_FMT_MEDIACODEC,
  385. AV_PIX_FMT_NONE,
  386. };
  387. atomic_init(&s->refcount, 1);
  388. atomic_init(&s->serial, 1);
  389. pix_fmt = ff_get_format(avctx, pix_fmts);
  390. if (pix_fmt == AV_PIX_FMT_MEDIACODEC) {
  391. AVMediaCodecContext *user_ctx = avctx->hwaccel_context;
  392. if (avctx->hw_device_ctx) {
  393. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)(avctx->hw_device_ctx->data);
  394. if (device_ctx->type == AV_HWDEVICE_TYPE_MEDIACODEC) {
  395. if (device_ctx->hwctx) {
  396. AVMediaCodecDeviceContext *mediacodec_ctx = (AVMediaCodecDeviceContext *)device_ctx->hwctx;
  397. s->surface = ff_mediacodec_surface_ref(mediacodec_ctx->surface, avctx);
  398. av_log(avctx, AV_LOG_INFO, "Using surface %p\n", s->surface);
  399. }
  400. }
  401. }
  402. if (!s->surface && user_ctx && user_ctx->surface) {
  403. s->surface = ff_mediacodec_surface_ref(user_ctx->surface, avctx);
  404. av_log(avctx, AV_LOG_INFO, "Using surface %p\n", s->surface);
  405. }
  406. }
  407. profile = ff_AMediaCodecProfile_getProfileFromAVCodecContext(avctx);
  408. if (profile < 0) {
  409. av_log(avctx, AV_LOG_WARNING, "Unsupported or unknown profile\n");
  410. }
  411. s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, profile, 0, avctx);
  412. if (!s->codec_name) {
  413. ret = AVERROR_EXTERNAL;
  414. goto fail;
  415. }
  416. av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name);
  417. s->codec = ff_AMediaCodec_createCodecByName(s->codec_name);
  418. if (!s->codec) {
  419. av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name);
  420. ret = AVERROR_EXTERNAL;
  421. goto fail;
  422. }
  423. status = ff_AMediaCodec_configure(s->codec, format, s->surface, NULL, 0);
  424. if (status < 0) {
  425. char *desc = ff_AMediaFormat_toString(format);
  426. av_log(avctx, AV_LOG_ERROR,
  427. "Failed to configure codec (status = %d) with format %s\n",
  428. status, desc);
  429. av_freep(&desc);
  430. ret = AVERROR_EXTERNAL;
  431. goto fail;
  432. }
  433. status = ff_AMediaCodec_start(s->codec);
  434. if (status < 0) {
  435. char *desc = ff_AMediaFormat_toString(format);
  436. av_log(avctx, AV_LOG_ERROR,
  437. "Failed to start codec (status = %d) with format %s\n",
  438. status, desc);
  439. av_freep(&desc);
  440. ret = AVERROR_EXTERNAL;
  441. goto fail;
  442. }
  443. s->format = ff_AMediaCodec_getOutputFormat(s->codec);
  444. if (s->format) {
  445. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  446. av_log(avctx, AV_LOG_ERROR,
  447. "Failed to configure context\n");
  448. goto fail;
  449. }
  450. }
  451. av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", s->codec);
  452. return 0;
  453. fail:
  454. av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", s->codec);
  455. ff_mediacodec_dec_close(avctx, s);
  456. return ret;
  457. }
  458. int ff_mediacodec_dec_send(AVCodecContext *avctx, MediaCodecDecContext *s,
  459. AVPacket *pkt)
  460. {
  461. int offset = 0;
  462. int need_draining = 0;
  463. uint8_t *data;
  464. ssize_t index;
  465. size_t size;
  466. FFAMediaCodec *codec = s->codec;
  467. int status;
  468. int64_t input_dequeue_timeout_us = INPUT_DEQUEUE_TIMEOUT_US;
  469. if (s->flushing) {
  470. av_log(avctx, AV_LOG_ERROR, "Decoder is flushing and cannot accept new buffer "
  471. "until all output buffers have been released\n");
  472. return AVERROR_EXTERNAL;
  473. }
  474. if (pkt->size == 0) {
  475. need_draining = 1;
  476. }
  477. if (s->draining && s->eos) {
  478. return AVERROR_EOF;
  479. }
  480. while (offset < pkt->size || (need_draining && !s->draining)) {
  481. index = ff_AMediaCodec_dequeueInputBuffer(codec, input_dequeue_timeout_us);
  482. if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  483. av_log(avctx, AV_LOG_TRACE, "Failed to dequeue input buffer, try again later..\n");
  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. av_log(avctx, AV_LOG_TRACE, "Queued input buffer %zd"
  508. " size=%zd ts=%" PRIi64 "\n", index, size, pts);
  509. s->draining = 1;
  510. break;
  511. } else {
  512. int64_t pts = pkt->pts;
  513. size = FFMIN(pkt->size - offset, size);
  514. memcpy(data, pkt->data + offset, size);
  515. offset += size;
  516. if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
  517. pts = av_rescale_q(pts, avctx->pkt_timebase, av_make_q(1, 1000000));
  518. }
  519. status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pts, 0);
  520. if (status < 0) {
  521. av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status);
  522. return AVERROR_EXTERNAL;
  523. }
  524. }
  525. }
  526. if (offset == 0)
  527. return AVERROR(EAGAIN);
  528. return offset;
  529. }
  530. int ff_mediacodec_dec_receive(AVCodecContext *avctx, MediaCodecDecContext *s,
  531. AVFrame *frame, bool wait)
  532. {
  533. int ret;
  534. uint8_t *data;
  535. ssize_t index;
  536. size_t size;
  537. FFAMediaCodec *codec = s->codec;
  538. FFAMediaCodecBufferInfo info = { 0 };
  539. int status;
  540. int64_t output_dequeue_timeout_us = OUTPUT_DEQUEUE_TIMEOUT_US;
  541. if (s->draining && s->eos) {
  542. return AVERROR_EOF;
  543. }
  544. if (s->draining) {
  545. /* If the codec is flushing or need to be flushed, block for a fair
  546. * amount of time to ensure we got a frame */
  547. output_dequeue_timeout_us = OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US;
  548. } else if (s->output_buffer_count == 0 || !wait) {
  549. /* If the codec hasn't produced any frames, do not block so we
  550. * can push data to it as fast as possible, and get the first
  551. * frame */
  552. output_dequeue_timeout_us = 0;
  553. }
  554. index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, output_dequeue_timeout_us);
  555. if (index >= 0) {
  556. av_log(avctx, AV_LOG_TRACE, "Got output buffer %zd"
  557. " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
  558. " flags=%" PRIu32 "\n", index, info.offset, info.size,
  559. info.presentationTimeUs, info.flags);
  560. if (info.flags & ff_AMediaCodec_getBufferFlagEndOfStream(codec)) {
  561. s->eos = 1;
  562. }
  563. if (info.size) {
  564. if (s->surface) {
  565. if ((ret = mediacodec_wrap_hw_buffer(avctx, s, index, &info, frame)) < 0) {
  566. av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
  567. return ret;
  568. }
  569. } else {
  570. data = ff_AMediaCodec_getOutputBuffer(codec, index, &size);
  571. if (!data) {
  572. av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n");
  573. return AVERROR_EXTERNAL;
  574. }
  575. if ((ret = mediacodec_wrap_sw_buffer(avctx, s, data, size, index, &info, frame)) < 0) {
  576. av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
  577. return ret;
  578. }
  579. }
  580. s->output_buffer_count++;
  581. return 0;
  582. } else {
  583. status = ff_AMediaCodec_releaseOutputBuffer(codec, index, 0);
  584. if (status < 0) {
  585. av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
  586. }
  587. }
  588. } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) {
  589. char *format = NULL;
  590. if (s->format) {
  591. status = ff_AMediaFormat_delete(s->format);
  592. if (status < 0) {
  593. av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format);
  594. }
  595. }
  596. s->format = ff_AMediaCodec_getOutputFormat(codec);
  597. if (!s->format) {
  598. av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n");
  599. return AVERROR_EXTERNAL;
  600. }
  601. format = ff_AMediaFormat_toString(s->format);
  602. if (!format) {
  603. return AVERROR_EXTERNAL;
  604. }
  605. av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format);
  606. av_freep(&format);
  607. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  608. return ret;
  609. }
  610. } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) {
  611. ff_AMediaCodec_cleanOutputBuffers(codec);
  612. } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  613. if (s->draining) {
  614. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer within %" PRIi64 "ms "
  615. "while draining remaining frames, output will probably lack frames\n",
  616. output_dequeue_timeout_us / 1000);
  617. } else {
  618. av_log(avctx, AV_LOG_DEBUG, "No output buffer available, try again later\n");
  619. }
  620. } else {
  621. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index);
  622. return AVERROR_EXTERNAL;
  623. }
  624. return AVERROR(EAGAIN);
  625. }
  626. int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s)
  627. {
  628. if (!s->surface || atomic_load(&s->refcount) == 1) {
  629. int ret;
  630. /* No frames (holding a reference to the codec) are retained by the
  631. * user, thus we can flush the codec and returns accordingly */
  632. if ((ret = mediacodec_dec_flush_codec(avctx, s)) < 0) {
  633. return ret;
  634. }
  635. return 1;
  636. }
  637. s->flushing = 1;
  638. return 0;
  639. }
  640. int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s)
  641. {
  642. ff_mediacodec_dec_unref(s);
  643. return 0;
  644. }
  645. int ff_mediacodec_dec_is_flushing(AVCodecContext *avctx, MediaCodecDecContext *s)
  646. {
  647. return s->flushing;
  648. }