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.

779 lines
24KB

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