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.

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