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.

835 lines
27KB

  1. /*
  2. * MMAL Video Decoder
  3. * Copyright (c) 2015 Rodger Combs
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * MMAL Video Decoder
  24. */
  25. #include <bcm_host.h>
  26. #include <interface/mmal/mmal.h>
  27. #include <interface/mmal/mmal_parameters_video.h>
  28. #include <interface/mmal/util/mmal_util.h>
  29. #include <interface/mmal/util/mmal_util_params.h>
  30. #include <interface/mmal/util/mmal_default_components.h>
  31. #include <interface/mmal/vc/mmal_vc_api.h>
  32. #include <stdatomic.h>
  33. #include "avcodec.h"
  34. #include "hwaccel.h"
  35. #include "internal.h"
  36. #include "libavutil/avassert.h"
  37. #include "libavutil/buffer.h"
  38. #include "libavutil/common.h"
  39. #include "libavutil/imgutils.h"
  40. #include "libavutil/opt.h"
  41. #include "libavutil/log.h"
  42. typedef struct FFBufferEntry {
  43. AVBufferRef *ref;
  44. void *data;
  45. size_t length;
  46. int64_t pts, dts;
  47. int flags;
  48. struct FFBufferEntry *next;
  49. } FFBufferEntry;
  50. // MMAL_POOL_T destroys all of its MMAL_BUFFER_HEADER_Ts. If we want correct
  51. // refcounting for AVFrames, we can free the MMAL_POOL_T only after all AVFrames
  52. // have been unreferenced.
  53. typedef struct FFPoolRef {
  54. atomic_int refcount;
  55. MMAL_POOL_T *pool;
  56. } FFPoolRef;
  57. typedef struct FFBufferRef {
  58. MMAL_BUFFER_HEADER_T *buffer;
  59. FFPoolRef *pool;
  60. } FFBufferRef;
  61. typedef struct MMALDecodeContext {
  62. AVClass *av_class;
  63. int extra_buffers;
  64. int extra_decoder_buffers;
  65. MMAL_COMPONENT_T *decoder;
  66. MMAL_QUEUE_T *queue_decoded_frames;
  67. MMAL_POOL_T *pool_in;
  68. FFPoolRef *pool_out;
  69. // Waiting input packets. Because the libavcodec API requires decoding and
  70. // returning packets in lockstep, it can happen that queue_decoded_frames
  71. // contains almost all surfaces - then the decoder input queue can quickly
  72. // fill up and won't accept new input either. Without consuming input, the
  73. // libavcodec API can't return new frames, and we have a logical deadlock.
  74. // This is avoided by queuing such buffers here.
  75. FFBufferEntry *waiting_buffers, *waiting_buffers_tail;
  76. int64_t packets_sent;
  77. atomic_int packets_buffered;
  78. int64_t frames_output;
  79. int eos_received;
  80. int eos_sent;
  81. int extradata_sent;
  82. } MMALDecodeContext;
  83. // Assume decoder is guaranteed to produce output after at least this many
  84. // packets (where each packet contains 1 frame).
  85. #define MAX_DELAYED_FRAMES 16
  86. static void ffmmal_poolref_unref(FFPoolRef *ref)
  87. {
  88. if (ref &&
  89. atomic_fetch_add_explicit(&ref->refcount, -1, memory_order_acq_rel) == 1) {
  90. mmal_pool_destroy(ref->pool);
  91. av_free(ref);
  92. }
  93. }
  94. static void ffmmal_release_frame(void *opaque, uint8_t *data)
  95. {
  96. FFBufferRef *ref = (void *)data;
  97. mmal_buffer_header_release(ref->buffer);
  98. ffmmal_poolref_unref(ref->pool);
  99. av_free(ref);
  100. }
  101. // Setup frame with a new reference to buffer. The buffer must have been
  102. // allocated from the given pool.
  103. static int ffmmal_set_ref(AVFrame *frame, FFPoolRef *pool,
  104. MMAL_BUFFER_HEADER_T *buffer)
  105. {
  106. FFBufferRef *ref = av_mallocz(sizeof(*ref));
  107. if (!ref)
  108. return AVERROR(ENOMEM);
  109. ref->pool = pool;
  110. ref->buffer = buffer;
  111. frame->buf[0] = av_buffer_create((void *)ref, sizeof(*ref),
  112. ffmmal_release_frame, NULL,
  113. AV_BUFFER_FLAG_READONLY);
  114. if (!frame->buf[0]) {
  115. av_free(ref);
  116. return AVERROR(ENOMEM);
  117. }
  118. atomic_fetch_add_explicit(&ref->pool->refcount, 1, memory_order_relaxed);
  119. mmal_buffer_header_acquire(buffer);
  120. frame->format = AV_PIX_FMT_MMAL;
  121. frame->data[3] = (uint8_t *)ref->buffer;
  122. return 0;
  123. }
  124. static void ffmmal_stop_decoder(AVCodecContext *avctx)
  125. {
  126. MMALDecodeContext *ctx = avctx->priv_data;
  127. MMAL_COMPONENT_T *decoder = ctx->decoder;
  128. MMAL_BUFFER_HEADER_T *buffer;
  129. mmal_port_disable(decoder->input[0]);
  130. mmal_port_disable(decoder->output[0]);
  131. mmal_port_disable(decoder->control);
  132. mmal_port_flush(decoder->input[0]);
  133. mmal_port_flush(decoder->output[0]);
  134. mmal_port_flush(decoder->control);
  135. while ((buffer = mmal_queue_get(ctx->queue_decoded_frames)))
  136. mmal_buffer_header_release(buffer);
  137. while (ctx->waiting_buffers) {
  138. FFBufferEntry *buffer = ctx->waiting_buffers;
  139. ctx->waiting_buffers = buffer->next;
  140. if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
  141. atomic_fetch_add(&ctx->packets_buffered, -1);
  142. av_buffer_unref(&buffer->ref);
  143. av_free(buffer);
  144. }
  145. ctx->waiting_buffers_tail = NULL;
  146. av_assert0(atomic_load(&ctx->packets_buffered) == 0);
  147. ctx->frames_output = ctx->eos_received = ctx->eos_sent = ctx->packets_sent = ctx->extradata_sent = 0;
  148. }
  149. static av_cold int ffmmal_close_decoder(AVCodecContext *avctx)
  150. {
  151. MMALDecodeContext *ctx = avctx->priv_data;
  152. if (ctx->decoder)
  153. ffmmal_stop_decoder(avctx);
  154. mmal_component_destroy(ctx->decoder);
  155. ctx->decoder = NULL;
  156. mmal_queue_destroy(ctx->queue_decoded_frames);
  157. mmal_pool_destroy(ctx->pool_in);
  158. ffmmal_poolref_unref(ctx->pool_out);
  159. mmal_vc_deinit();
  160. return 0;
  161. }
  162. static void input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  163. {
  164. AVCodecContext *avctx = (AVCodecContext*)port->userdata;
  165. MMALDecodeContext *ctx = avctx->priv_data;
  166. if (!buffer->cmd) {
  167. FFBufferEntry *entry = buffer->user_data;
  168. av_buffer_unref(&entry->ref);
  169. if (entry->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
  170. atomic_fetch_add(&ctx->packets_buffered, -1);
  171. av_free(entry);
  172. }
  173. mmal_buffer_header_release(buffer);
  174. }
  175. static void output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  176. {
  177. AVCodecContext *avctx = (AVCodecContext*)port->userdata;
  178. MMALDecodeContext *ctx = avctx->priv_data;
  179. mmal_queue_put(ctx->queue_decoded_frames, buffer);
  180. }
  181. static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  182. {
  183. AVCodecContext *avctx = (AVCodecContext*)port->userdata;
  184. MMAL_STATUS_T status;
  185. if (buffer->cmd == MMAL_EVENT_ERROR) {
  186. status = *(uint32_t *)buffer->data;
  187. av_log(avctx, AV_LOG_ERROR, "MMAL error %d on control port\n", (int)status);
  188. } else {
  189. char s[20];
  190. av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
  191. av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on control port\n", s);
  192. }
  193. mmal_buffer_header_release(buffer);
  194. }
  195. // Feed free output buffers to the decoder.
  196. static int ffmmal_fill_output_port(AVCodecContext *avctx)
  197. {
  198. MMALDecodeContext *ctx = avctx->priv_data;
  199. MMAL_BUFFER_HEADER_T *buffer;
  200. MMAL_STATUS_T status;
  201. if (!ctx->pool_out)
  202. return AVERROR_UNKNOWN; // format change code failed with OOM previously
  203. while ((buffer = mmal_queue_get(ctx->pool_out->pool->queue))) {
  204. if ((status = mmal_port_send_buffer(ctx->decoder->output[0], buffer))) {
  205. mmal_buffer_header_release(buffer);
  206. av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending output buffer.\n", (int)status);
  207. return AVERROR_UNKNOWN;
  208. }
  209. }
  210. return 0;
  211. }
  212. static enum AVColorSpace ffmmal_csp_to_av_csp(MMAL_FOURCC_T fourcc)
  213. {
  214. switch (fourcc) {
  215. case MMAL_COLOR_SPACE_BT470_2_BG:
  216. case MMAL_COLOR_SPACE_BT470_2_M:
  217. case MMAL_COLOR_SPACE_ITUR_BT601: return AVCOL_SPC_BT470BG;
  218. case MMAL_COLOR_SPACE_ITUR_BT709: return AVCOL_SPC_BT709;
  219. case MMAL_COLOR_SPACE_FCC: return AVCOL_SPC_FCC;
  220. case MMAL_COLOR_SPACE_SMPTE240M: return AVCOL_SPC_SMPTE240M;
  221. default: return AVCOL_SPC_UNSPECIFIED;
  222. }
  223. }
  224. static int ffmal_update_format(AVCodecContext *avctx)
  225. {
  226. MMALDecodeContext *ctx = avctx->priv_data;
  227. MMAL_STATUS_T status;
  228. int ret = 0;
  229. MMAL_COMPONENT_T *decoder = ctx->decoder;
  230. MMAL_ES_FORMAT_T *format_out = decoder->output[0]->format;
  231. ffmmal_poolref_unref(ctx->pool_out);
  232. if (!(ctx->pool_out = av_mallocz(sizeof(*ctx->pool_out)))) {
  233. ret = AVERROR(ENOMEM);
  234. goto fail;
  235. }
  236. atomic_init(&ctx->pool_out->refcount, 1);
  237. if (!format_out)
  238. goto fail;
  239. if ((status = mmal_port_parameter_set_uint32(decoder->output[0], MMAL_PARAMETER_EXTRA_BUFFERS, ctx->extra_buffers)))
  240. goto fail;
  241. if ((status = mmal_port_parameter_set_boolean(decoder->output[0], MMAL_PARAMETER_VIDEO_INTERPOLATE_TIMESTAMPS, 0)))
  242. goto fail;
  243. if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
  244. format_out->encoding = MMAL_ENCODING_OPAQUE;
  245. } else {
  246. format_out->encoding_variant = format_out->encoding = MMAL_ENCODING_I420;
  247. }
  248. if ((status = mmal_port_format_commit(decoder->output[0])))
  249. goto fail;
  250. if ((ret = ff_set_dimensions(avctx, format_out->es->video.crop.x + format_out->es->video.crop.width,
  251. format_out->es->video.crop.y + format_out->es->video.crop.height)) < 0)
  252. goto fail;
  253. if (format_out->es->video.par.num && format_out->es->video.par.den) {
  254. avctx->sample_aspect_ratio.num = format_out->es->video.par.num;
  255. avctx->sample_aspect_ratio.den = format_out->es->video.par.den;
  256. }
  257. avctx->colorspace = ffmmal_csp_to_av_csp(format_out->es->video.color_space);
  258. decoder->output[0]->buffer_size =
  259. FFMAX(decoder->output[0]->buffer_size_min, decoder->output[0]->buffer_size_recommended);
  260. decoder->output[0]->buffer_num =
  261. FFMAX(decoder->output[0]->buffer_num_min, decoder->output[0]->buffer_num_recommended) + ctx->extra_buffers;
  262. ctx->pool_out->pool = mmal_pool_create(decoder->output[0]->buffer_num,
  263. decoder->output[0]->buffer_size);
  264. if (!ctx->pool_out->pool) {
  265. ret = AVERROR(ENOMEM);
  266. goto fail;
  267. }
  268. return 0;
  269. fail:
  270. return ret < 0 ? ret : AVERROR_UNKNOWN;
  271. }
  272. static av_cold int ffmmal_init_decoder(AVCodecContext *avctx)
  273. {
  274. MMALDecodeContext *ctx = avctx->priv_data;
  275. MMAL_STATUS_T status;
  276. MMAL_ES_FORMAT_T *format_in;
  277. MMAL_COMPONENT_T *decoder;
  278. char tmp[32];
  279. int ret = 0;
  280. bcm_host_init();
  281. if (mmal_vc_init()) {
  282. av_log(avctx, AV_LOG_ERROR, "Cannot initialize MMAL VC driver!\n");
  283. return AVERROR(ENOSYS);
  284. }
  285. if ((ret = ff_get_format(avctx, avctx->codec->pix_fmts)) < 0)
  286. return ret;
  287. avctx->pix_fmt = ret;
  288. if ((status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_DECODER, &ctx->decoder)))
  289. goto fail;
  290. decoder = ctx->decoder;
  291. format_in = decoder->input[0]->format;
  292. format_in->type = MMAL_ES_TYPE_VIDEO;
  293. switch (avctx->codec_id) {
  294. case AV_CODEC_ID_MPEG2VIDEO:
  295. format_in->encoding = MMAL_ENCODING_MP2V;
  296. break;
  297. case AV_CODEC_ID_VC1:
  298. format_in->encoding = MMAL_ENCODING_WVC1;
  299. break;
  300. case AV_CODEC_ID_H264:
  301. default:
  302. format_in->encoding = MMAL_ENCODING_H264;
  303. break;
  304. }
  305. format_in->es->video.width = FFALIGN(avctx->width, 32);
  306. format_in->es->video.height = FFALIGN(avctx->height, 16);
  307. format_in->es->video.crop.width = avctx->width;
  308. format_in->es->video.crop.height = avctx->height;
  309. format_in->es->video.frame_rate.num = 24000;
  310. format_in->es->video.frame_rate.den = 1001;
  311. format_in->es->video.par.num = avctx->sample_aspect_ratio.num;
  312. format_in->es->video.par.den = avctx->sample_aspect_ratio.den;
  313. format_in->flags = MMAL_ES_FORMAT_FLAG_FRAMED;
  314. av_get_codec_tag_string(tmp, sizeof(tmp), format_in->encoding);
  315. av_log(avctx, AV_LOG_DEBUG, "Using MMAL %s encoding.\n", tmp);
  316. #if HAVE_MMAL_PARAMETER_VIDEO_MAX_NUM_CALLBACKS
  317. if (mmal_port_parameter_set_uint32(decoder->input[0], MMAL_PARAMETER_VIDEO_MAX_NUM_CALLBACKS,
  318. -1 - ctx->extra_decoder_buffers)) {
  319. av_log(avctx, AV_LOG_WARNING, "Could not set input buffering limit.\n");
  320. }
  321. #endif
  322. if ((status = mmal_port_format_commit(decoder->input[0])))
  323. goto fail;
  324. decoder->input[0]->buffer_num =
  325. FFMAX(decoder->input[0]->buffer_num_min, 20);
  326. decoder->input[0]->buffer_size =
  327. FFMAX(decoder->input[0]->buffer_size_min, 512 * 1024);
  328. ctx->pool_in = mmal_pool_create(decoder->input[0]->buffer_num, 0);
  329. if (!ctx->pool_in) {
  330. ret = AVERROR(ENOMEM);
  331. goto fail;
  332. }
  333. if ((ret = ffmal_update_format(avctx)) < 0)
  334. goto fail;
  335. ctx->queue_decoded_frames = mmal_queue_create();
  336. if (!ctx->queue_decoded_frames)
  337. goto fail;
  338. decoder->input[0]->userdata = (void*)avctx;
  339. decoder->output[0]->userdata = (void*)avctx;
  340. decoder->control->userdata = (void*)avctx;
  341. if ((status = mmal_port_enable(decoder->control, control_port_cb)))
  342. goto fail;
  343. if ((status = mmal_port_enable(decoder->input[0], input_callback)))
  344. goto fail;
  345. if ((status = mmal_port_enable(decoder->output[0], output_callback)))
  346. goto fail;
  347. if ((status = mmal_component_enable(decoder)))
  348. goto fail;
  349. return 0;
  350. fail:
  351. ffmmal_close_decoder(avctx);
  352. return ret < 0 ? ret : AVERROR_UNKNOWN;
  353. }
  354. static void ffmmal_flush(AVCodecContext *avctx)
  355. {
  356. MMALDecodeContext *ctx = avctx->priv_data;
  357. MMAL_COMPONENT_T *decoder = ctx->decoder;
  358. MMAL_STATUS_T status;
  359. ffmmal_stop_decoder(avctx);
  360. if ((status = mmal_port_enable(decoder->control, control_port_cb)))
  361. goto fail;
  362. if ((status = mmal_port_enable(decoder->input[0], input_callback)))
  363. goto fail;
  364. if ((status = mmal_port_enable(decoder->output[0], output_callback)))
  365. goto fail;
  366. return;
  367. fail:
  368. av_log(avctx, AV_LOG_ERROR, "MMAL flush error: %i\n", (int)status);
  369. }
  370. // Split packets and add them to the waiting_buffers list. We don't queue them
  371. // immediately, because it can happen that the decoder is temporarily blocked
  372. // (due to us not reading/returning enough output buffers) and won't accept
  373. // new input. (This wouldn't be an issue if MMAL input buffers always were
  374. // complete frames - then the input buffer just would have to be big enough.)
  375. // If is_extradata is set, send it as MMAL_BUFFER_HEADER_FLAG_CONFIG.
  376. static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt,
  377. int is_extradata)
  378. {
  379. MMALDecodeContext *ctx = avctx->priv_data;
  380. AVBufferRef *buf = NULL;
  381. int size = 0;
  382. uint8_t *data = (uint8_t *)"";
  383. uint8_t *start;
  384. int ret = 0;
  385. if (avpkt->size) {
  386. if (avpkt->buf) {
  387. buf = av_buffer_ref(avpkt->buf);
  388. size = avpkt->size;
  389. data = avpkt->data;
  390. } else {
  391. buf = av_buffer_alloc(avpkt->size);
  392. if (buf) {
  393. memcpy(buf->data, avpkt->data, avpkt->size);
  394. size = buf->size;
  395. data = buf->data;
  396. }
  397. }
  398. if (!buf) {
  399. ret = AVERROR(ENOMEM);
  400. goto done;
  401. }
  402. if (!is_extradata)
  403. ctx->packets_sent++;
  404. } else {
  405. if (ctx->eos_sent)
  406. goto done;
  407. if (!ctx->packets_sent) {
  408. // Short-cut the flush logic to avoid upsetting MMAL.
  409. ctx->eos_sent = 1;
  410. ctx->eos_received = 1;
  411. goto done;
  412. }
  413. }
  414. start = data;
  415. do {
  416. FFBufferEntry *buffer = av_mallocz(sizeof(*buffer));
  417. if (!buffer) {
  418. ret = AVERROR(ENOMEM);
  419. goto done;
  420. }
  421. buffer->data = data;
  422. buffer->length = FFMIN(size, ctx->decoder->input[0]->buffer_size);
  423. if (is_extradata)
  424. buffer->flags |= MMAL_BUFFER_HEADER_FLAG_CONFIG;
  425. if (data == start)
  426. buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_START;
  427. data += buffer->length;
  428. size -= buffer->length;
  429. buffer->pts = avpkt->pts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->pts;
  430. buffer->dts = avpkt->dts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->dts;
  431. if (!size) {
  432. buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
  433. atomic_fetch_add(&ctx->packets_buffered, 1);
  434. }
  435. if (!buffer->length) {
  436. buffer->flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
  437. ctx->eos_sent = 1;
  438. }
  439. if (buf) {
  440. buffer->ref = av_buffer_ref(buf);
  441. if (!buffer->ref) {
  442. av_free(buffer);
  443. ret = AVERROR(ENOMEM);
  444. goto done;
  445. }
  446. }
  447. // Insert at end of the list
  448. if (!ctx->waiting_buffers)
  449. ctx->waiting_buffers = buffer;
  450. if (ctx->waiting_buffers_tail)
  451. ctx->waiting_buffers_tail->next = buffer;
  452. ctx->waiting_buffers_tail = buffer;
  453. } while (size);
  454. done:
  455. av_buffer_unref(&buf);
  456. return ret;
  457. }
  458. // Move prepared/split packets from waiting_buffers to the MMAL decoder.
  459. static int ffmmal_fill_input_port(AVCodecContext *avctx)
  460. {
  461. MMALDecodeContext *ctx = avctx->priv_data;
  462. while (ctx->waiting_buffers) {
  463. MMAL_BUFFER_HEADER_T *mbuffer;
  464. FFBufferEntry *buffer;
  465. MMAL_STATUS_T status;
  466. mbuffer = mmal_queue_get(ctx->pool_in->queue);
  467. if (!mbuffer)
  468. return 0;
  469. buffer = ctx->waiting_buffers;
  470. mmal_buffer_header_reset(mbuffer);
  471. mbuffer->cmd = 0;
  472. mbuffer->pts = buffer->pts;
  473. mbuffer->dts = buffer->dts;
  474. mbuffer->flags = buffer->flags;
  475. mbuffer->data = buffer->data;
  476. mbuffer->length = buffer->length;
  477. mbuffer->user_data = buffer;
  478. mbuffer->alloc_size = ctx->decoder->input[0]->buffer_size;
  479. // Remove from start of the list
  480. ctx->waiting_buffers = buffer->next;
  481. if (ctx->waiting_buffers_tail == buffer)
  482. ctx->waiting_buffers_tail = NULL;
  483. if ((status = mmal_port_send_buffer(ctx->decoder->input[0], mbuffer))) {
  484. mmal_buffer_header_release(mbuffer);
  485. av_buffer_unref(&buffer->ref);
  486. if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
  487. atomic_fetch_add(&ctx->packets_buffered, -1);
  488. av_free(buffer);
  489. }
  490. if (status) {
  491. av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending input\n", (int)status);
  492. return AVERROR_UNKNOWN;
  493. }
  494. }
  495. return 0;
  496. }
  497. static int ffmal_copy_frame(AVCodecContext *avctx, AVFrame *frame,
  498. MMAL_BUFFER_HEADER_T *buffer)
  499. {
  500. MMALDecodeContext *ctx = avctx->priv_data;
  501. int ret = 0;
  502. if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
  503. if (!ctx->pool_out)
  504. return AVERROR_UNKNOWN; // format change code failed with OOM previously
  505. if ((ret = ff_decode_frame_props(avctx, frame)) < 0)
  506. goto done;
  507. if ((ret = ffmmal_set_ref(frame, ctx->pool_out, buffer)) < 0)
  508. goto done;
  509. } else {
  510. int w = FFALIGN(avctx->width, 32);
  511. int h = FFALIGN(avctx->height, 16);
  512. uint8_t *src[4];
  513. int linesize[4];
  514. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  515. goto done;
  516. av_image_fill_arrays(src, linesize,
  517. buffer->data + buffer->type->video.offset[0],
  518. avctx->pix_fmt, w, h, 1);
  519. av_image_copy(frame->data, frame->linesize, src, linesize,
  520. avctx->pix_fmt, avctx->width, avctx->height);
  521. }
  522. frame->pts = buffer->pts == MMAL_TIME_UNKNOWN ? AV_NOPTS_VALUE : buffer->pts;
  523. #if FF_API_PKT_PTS
  524. FF_DISABLE_DEPRECATION_WARNINGS
  525. frame->pkt_pts = frame->pts;
  526. FF_ENABLE_DEPRECATION_WARNINGS
  527. #endif
  528. frame->pkt_dts = AV_NOPTS_VALUE;
  529. done:
  530. return ret;
  531. }
  532. // Fetch a decoded buffer and place it into the frame parameter.
  533. static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
  534. {
  535. MMALDecodeContext *ctx = avctx->priv_data;
  536. MMAL_BUFFER_HEADER_T *buffer = NULL;
  537. MMAL_STATUS_T status = 0;
  538. int ret = 0;
  539. if (ctx->eos_received)
  540. goto done;
  541. while (1) {
  542. // To ensure decoding in lockstep with a constant delay between fed packets
  543. // and output frames, we always wait until an output buffer is available.
  544. // Except during start we don't know after how many input packets the decoder
  545. // is going to return the first buffer, and we can't distinguish decoder
  546. // being busy from decoder waiting for input. So just poll at the start and
  547. // keep feeding new data to the buffer.
  548. // We are pretty sure the decoder will produce output if we sent more input
  549. // frames than what a H.264 decoder could logically delay. This avoids too
  550. // excessive buffering.
  551. // We also wait if we sent eos, but didn't receive it yet (think of decoding
  552. // stream with a very low number of frames).
  553. if (atomic_load(&ctx->packets_buffered) > MAX_DELAYED_FRAMES ||
  554. (ctx->packets_sent && ctx->eos_sent)) {
  555. // MMAL will ignore broken input packets, which means the frame we
  556. // expect here may never arrive. Dealing with this correctly is
  557. // complicated, so here's a hack to avoid that it freezes forever
  558. // in this unlikely situation.
  559. buffer = mmal_queue_timedwait(ctx->queue_decoded_frames, 100);
  560. if (!buffer) {
  561. av_log(avctx, AV_LOG_ERROR, "Did not get output frame from MMAL.\n");
  562. ret = AVERROR_UNKNOWN;
  563. goto done;
  564. }
  565. } else {
  566. buffer = mmal_queue_get(ctx->queue_decoded_frames);
  567. if (!buffer)
  568. goto done;
  569. }
  570. ctx->eos_received |= !!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS);
  571. if (ctx->eos_received)
  572. goto done;
  573. if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED) {
  574. MMAL_COMPONENT_T *decoder = ctx->decoder;
  575. MMAL_EVENT_FORMAT_CHANGED_T *ev = mmal_event_format_changed_get(buffer);
  576. MMAL_BUFFER_HEADER_T *stale_buffer;
  577. av_log(avctx, AV_LOG_INFO, "Changing output format.\n");
  578. if ((status = mmal_port_disable(decoder->output[0])))
  579. goto done;
  580. while ((stale_buffer = mmal_queue_get(ctx->queue_decoded_frames)))
  581. mmal_buffer_header_release(stale_buffer);
  582. mmal_format_copy(decoder->output[0]->format, ev->format);
  583. if ((ret = ffmal_update_format(avctx)) < 0)
  584. goto done;
  585. if ((status = mmal_port_enable(decoder->output[0], output_callback)))
  586. goto done;
  587. if ((ret = ffmmal_fill_output_port(avctx)) < 0)
  588. goto done;
  589. if ((ret = ffmmal_fill_input_port(avctx)) < 0)
  590. goto done;
  591. mmal_buffer_header_release(buffer);
  592. continue;
  593. } else if (buffer->cmd) {
  594. char s[20];
  595. av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
  596. av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on output port\n", s);
  597. goto done;
  598. } else if (buffer->length == 0) {
  599. // Unused output buffer that got drained after format change.
  600. mmal_buffer_header_release(buffer);
  601. continue;
  602. }
  603. ctx->frames_output++;
  604. if ((ret = ffmal_copy_frame(avctx, frame, buffer)) < 0)
  605. goto done;
  606. *got_frame = 1;
  607. break;
  608. }
  609. done:
  610. if (buffer)
  611. mmal_buffer_header_release(buffer);
  612. if (status && ret >= 0)
  613. ret = AVERROR_UNKNOWN;
  614. return ret;
  615. }
  616. static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame,
  617. AVPacket *avpkt)
  618. {
  619. MMALDecodeContext *ctx = avctx->priv_data;
  620. AVFrame *frame = data;
  621. int ret = 0;
  622. if (avctx->extradata_size && !ctx->extradata_sent) {
  623. AVPacket pkt = {0};
  624. av_init_packet(&pkt);
  625. pkt.data = avctx->extradata;
  626. pkt.size = avctx->extradata_size;
  627. ctx->extradata_sent = 1;
  628. if ((ret = ffmmal_add_packet(avctx, &pkt, 1)) < 0)
  629. return ret;
  630. }
  631. if ((ret = ffmmal_add_packet(avctx, avpkt, 0)) < 0)
  632. return ret;
  633. if ((ret = ffmmal_fill_input_port(avctx)) < 0)
  634. return ret;
  635. if ((ret = ffmmal_fill_output_port(avctx)) < 0)
  636. return ret;
  637. if ((ret = ffmmal_read_frame(avctx, frame, got_frame)) < 0)
  638. return ret;
  639. // ffmmal_read_frame() can block for a while. Since the decoder is
  640. // asynchronous, it's a good idea to fill the ports again.
  641. if ((ret = ffmmal_fill_output_port(avctx)) < 0)
  642. return ret;
  643. if ((ret = ffmmal_fill_input_port(avctx)) < 0)
  644. return ret;
  645. return ret;
  646. }
  647. static const AVCodecHWConfigInternal *mmal_hw_configs[] = {
  648. HW_CONFIG_INTERNAL(MMAL),
  649. NULL
  650. };
  651. static const AVOption options[]={
  652. {"extra_buffers", "extra buffers", offsetof(MMALDecodeContext, extra_buffers), AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
  653. {"extra_decoder_buffers", "extra MMAL internal buffered frames", offsetof(MMALDecodeContext, extra_decoder_buffers), AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
  654. {NULL}
  655. };
  656. #define FFMMAL_DEC_CLASS(NAME) \
  657. static const AVClass ffmmal_##NAME##_dec_class = { \
  658. .class_name = "mmal_" #NAME "_dec", \
  659. .item_name = av_default_item_name, \
  660. .option = options, \
  661. .version = LIBAVUTIL_VERSION_INT, \
  662. };
  663. #define FFMMAL_DEC(NAME, ID) \
  664. FFMMAL_DEC_CLASS(NAME) \
  665. AVCodec ff_##NAME##_mmal_decoder = { \
  666. .name = #NAME "_mmal", \
  667. .long_name = NULL_IF_CONFIG_SMALL(#NAME " (mmal)"), \
  668. .type = AVMEDIA_TYPE_VIDEO, \
  669. .id = ID, \
  670. .priv_data_size = sizeof(MMALDecodeContext), \
  671. .init = ffmmal_init_decoder, \
  672. .close = ffmmal_close_decoder, \
  673. .decode = ffmmal_decode, \
  674. .flush = ffmmal_flush, \
  675. .priv_class = &ffmmal_##NAME##_dec_class, \
  676. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE, \
  677. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
  678. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MMAL, \
  679. AV_PIX_FMT_YUV420P, \
  680. AV_PIX_FMT_NONE}, \
  681. .hw_configs = mmal_hw_configs, \
  682. .wrapper_name = "mmal", \
  683. };
  684. FFMMAL_DEC(h264, AV_CODEC_ID_H264)
  685. FFMMAL_DEC(mpeg2, AV_CODEC_ID_MPEG2VIDEO)
  686. FFMMAL_DEC(vc1, AV_CODEC_ID_VC1)