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.

791 lines
25KB

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