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.

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