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.

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