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.

793 lines
25KB

  1. /*
  2. * MMAL Video Decoder
  3. * Copyright (c) 2015 Rodger Combs
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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, "private_spspps_buf", &dummy_p, &dummy_int, NULL, 0, 0);
  298. } else if (avctx->extradata_size) {
  299. if ((status = mmal_format_extradata_alloc(format_in, avctx->extradata_size)))
  300. goto fail;
  301. format_in->extradata_size = avctx->extradata_size;
  302. memcpy(format_in->extradata, avctx->extradata, format_in->extradata_size);
  303. }
  304. if ((status = mmal_port_format_commit(decoder->input[0])))
  305. goto fail;
  306. decoder->input[0]->buffer_num =
  307. FFMAX(decoder->input[0]->buffer_num_min, 20);
  308. decoder->input[0]->buffer_size =
  309. FFMAX(decoder->input[0]->buffer_size_min, 512 * 1024);
  310. ctx->pool_in = mmal_pool_create(decoder->input[0]->buffer_num, 0);
  311. if (!ctx->pool_in) {
  312. ret = AVERROR(ENOMEM);
  313. goto fail;
  314. }
  315. if ((ret = ffmal_update_format(avctx)) < 0)
  316. goto fail;
  317. ctx->queue_decoded_frames = mmal_queue_create();
  318. if (!ctx->queue_decoded_frames)
  319. goto fail;
  320. decoder->input[0]->userdata = (void*)avctx;
  321. decoder->output[0]->userdata = (void*)avctx;
  322. decoder->control->userdata = (void*)avctx;
  323. if ((status = mmal_port_enable(decoder->control, control_port_cb)))
  324. goto fail;
  325. if ((status = mmal_port_enable(decoder->input[0], input_callback)))
  326. goto fail;
  327. if ((status = mmal_port_enable(decoder->output[0], output_callback)))
  328. goto fail;
  329. if ((status = mmal_component_enable(decoder)))
  330. goto fail;
  331. return 0;
  332. fail:
  333. ffmmal_close_decoder(avctx);
  334. return ret < 0 ? ret : AVERROR_UNKNOWN;
  335. }
  336. static void ffmmal_flush(AVCodecContext *avctx)
  337. {
  338. MMALDecodeContext *ctx = avctx->priv_data;
  339. MMAL_COMPONENT_T *decoder = ctx->decoder;
  340. MMAL_STATUS_T status;
  341. ffmmal_stop_decoder(avctx);
  342. if ((status = mmal_port_enable(decoder->control, control_port_cb)))
  343. goto fail;
  344. if ((status = mmal_port_enable(decoder->input[0], input_callback)))
  345. goto fail;
  346. if ((status = mmal_port_enable(decoder->output[0], output_callback)))
  347. goto fail;
  348. return;
  349. fail:
  350. av_log(avctx, AV_LOG_ERROR, "MMAL flush error: %i\n", (int)status);
  351. }
  352. // Split packets and add them to the waiting_buffers list. We don't queue them
  353. // immediately, because it can happen that the decoder is temporarily blocked
  354. // (due to us not reading/returning enough output buffers) and won't accept
  355. // new input. (This wouldn't be an issue if MMAL input buffers always were
  356. // complete frames - then the input buffer just would have to be big enough.)
  357. static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt)
  358. {
  359. MMALDecodeContext *ctx = avctx->priv_data;
  360. AVBufferRef *buf = NULL;
  361. int size = 0;
  362. uint8_t *data = (uint8_t *)"";
  363. uint8_t *start;
  364. int ret = 0;
  365. if (avpkt->size) {
  366. if (ctx->bsfc) {
  367. uint8_t *tmp_data;
  368. int tmp_size;
  369. if ((ret = av_bitstream_filter_filter(ctx->bsfc, avctx, "private_spspps_buf",
  370. &tmp_data, &tmp_size,
  371. avpkt->data, avpkt->size,
  372. avpkt->flags & AV_PKT_FLAG_KEY)) < 0)
  373. goto done;
  374. buf = av_buffer_create(tmp_data, tmp_size, NULL, NULL, 0);
  375. } else {
  376. if (avpkt->buf) {
  377. buf = av_buffer_ref(avpkt->buf);
  378. } else {
  379. buf = av_buffer_alloc(avpkt->size);
  380. if (buf)
  381. memcpy(buf->data, avpkt->data, avpkt->size);
  382. }
  383. }
  384. if (!buf) {
  385. ret = AVERROR(ENOMEM);
  386. goto done;
  387. }
  388. size = buf->size;
  389. data = buf->data;
  390. ctx->packets_sent++;
  391. } else {
  392. if (!ctx->packets_sent) {
  393. // Short-cut the flush logic to avoid upsetting MMAL.
  394. ctx->eos_sent = 1;
  395. ctx->eos_received = 1;
  396. goto done;
  397. }
  398. }
  399. start = data;
  400. do {
  401. FFBufferEntry *buffer = av_mallocz(sizeof(*buffer));
  402. if (!buffer) {
  403. ret = AVERROR(ENOMEM);
  404. goto done;
  405. }
  406. buffer->data = data;
  407. buffer->length = FFMIN(size, ctx->decoder->input[0]->buffer_size);
  408. if (data == start)
  409. buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_START;
  410. data += buffer->length;
  411. size -= buffer->length;
  412. buffer->pts = avpkt->pts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->pts;
  413. buffer->dts = avpkt->dts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->dts;
  414. if (!size)
  415. buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
  416. if (!buffer->length) {
  417. buffer->flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
  418. ctx->eos_sent = 1;
  419. }
  420. if (buf) {
  421. buffer->ref = av_buffer_ref(buf);
  422. if (!buffer->ref) {
  423. av_free(buffer);
  424. ret = AVERROR(ENOMEM);
  425. goto done;
  426. }
  427. }
  428. // Insert at end of the list
  429. if (!ctx->waiting_buffers)
  430. ctx->waiting_buffers = buffer;
  431. if (ctx->waiting_buffers_tail)
  432. ctx->waiting_buffers_tail->next = buffer;
  433. ctx->waiting_buffers_tail = buffer;
  434. } while (size);
  435. done:
  436. av_buffer_unref(&buf);
  437. return ret;
  438. }
  439. // Move prepared/split packets from waiting_buffers to the MMAL decoder.
  440. static int ffmmal_fill_input_port(AVCodecContext *avctx)
  441. {
  442. MMALDecodeContext *ctx = avctx->priv_data;
  443. while (ctx->waiting_buffers) {
  444. MMAL_BUFFER_HEADER_T *mbuffer;
  445. FFBufferEntry *buffer;
  446. MMAL_STATUS_T status;
  447. mbuffer = mmal_queue_get(ctx->pool_in->queue);
  448. if (!mbuffer)
  449. return 0;
  450. buffer = ctx->waiting_buffers;
  451. mmal_buffer_header_reset(mbuffer);
  452. mbuffer->cmd = 0;
  453. mbuffer->pts = buffer->pts;
  454. mbuffer->dts = buffer->dts;
  455. mbuffer->flags = buffer->flags;
  456. mbuffer->data = buffer->data;
  457. mbuffer->length = buffer->length;
  458. mbuffer->user_data = buffer->ref;
  459. mbuffer->alloc_size = ctx->decoder->input[0]->buffer_size;
  460. if ((status = mmal_port_send_buffer(ctx->decoder->input[0], mbuffer))) {
  461. mmal_buffer_header_release(mbuffer);
  462. av_buffer_unref(&buffer->ref);
  463. }
  464. // Remove from start of the list
  465. ctx->waiting_buffers = buffer->next;
  466. if (ctx->waiting_buffers_tail == buffer)
  467. ctx->waiting_buffers_tail = NULL;
  468. av_free(buffer);
  469. if (status) {
  470. av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending input\n", (int)status);
  471. return AVERROR_UNKNOWN;
  472. }
  473. }
  474. return 0;
  475. }
  476. static int ffmal_copy_frame(AVCodecContext *avctx, AVFrame *frame,
  477. MMAL_BUFFER_HEADER_T *buffer)
  478. {
  479. MMALDecodeContext *ctx = avctx->priv_data;
  480. int ret = 0;
  481. if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
  482. if (!ctx->pool_out)
  483. return AVERROR_UNKNOWN; // format change code failed with OOM previously
  484. if ((ret = ff_decode_frame_props(avctx, frame)) < 0)
  485. goto done;
  486. if ((ret = ffmmal_set_ref(frame, ctx->pool_out, buffer)) < 0)
  487. goto done;
  488. } else {
  489. int w = FFALIGN(avctx->width, 32);
  490. int h = FFALIGN(avctx->height, 16);
  491. char *ptr;
  492. int plane;
  493. int i;
  494. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  495. goto done;
  496. ptr = buffer->data + buffer->type->video.offset[0];
  497. for (i = 0; i < avctx->height; i++)
  498. memcpy(frame->data[0] + frame->linesize[0] * i, ptr + w * i, avctx->width);
  499. ptr += w * h;
  500. for (plane = 1; plane < 3; plane++) {
  501. for (i = 0; i < avctx->height / 2; i++)
  502. memcpy(frame->data[plane] + frame->linesize[plane] * i, ptr + w / 2 * i, (avctx->width + 1) / 2);
  503. ptr += w / 2 * h / 2;
  504. }
  505. }
  506. if (buffer->pts != MMAL_TIME_UNKNOWN) {
  507. frame->pkt_pts = buffer->pts;
  508. frame->pts = buffer->pts;
  509. }
  510. done:
  511. return ret;
  512. }
  513. // Fetch a decoded buffer and place it into the frame parameter.
  514. static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
  515. {
  516. MMALDecodeContext *ctx = avctx->priv_data;
  517. MMAL_BUFFER_HEADER_T *buffer = NULL;
  518. MMAL_STATUS_T status = 0;
  519. int ret = 0;
  520. if (ctx->eos_received)
  521. goto done;
  522. while (1) {
  523. // To ensure decoding in lockstep with a constant delay between fed packets
  524. // and output frames, we always wait until an output buffer is available.
  525. // Except during start we don't know after how many input packets the decoder
  526. // is going to return the first buffer, and we can't distinguish decoder
  527. // being busy from decoder waiting for input. So just poll at the start and
  528. // keep feeding new data to the buffer.
  529. // We are pretty sure the decoder will produce output if we sent more input
  530. // frames than what a h264 decoder could logically delay. This avoids too
  531. // excessive buffering.
  532. // We also wait if we sent eos, but didn't receive it yet (think of decoding
  533. // stream with a very low number of frames).
  534. if (ctx->frames_output || ctx->packets_sent > MAX_DELAYED_FRAMES ||
  535. (ctx->packets_sent && ctx->eos_sent)) {
  536. // MMAL will ignore broken input packets, which means the frame we
  537. // expect here may never arrive. Dealing with this correctly is
  538. // complicated, so here's a hack to avoid that it freezes forever
  539. // in this unlikely situation.
  540. buffer = mmal_queue_timedwait(ctx->queue_decoded_frames, 100);
  541. if (!buffer) {
  542. av_log(avctx, AV_LOG_ERROR, "Did not get output frame from MMAL.\n");
  543. ret = AVERROR_UNKNOWN;
  544. goto done;
  545. }
  546. } else {
  547. buffer = mmal_queue_get(ctx->queue_decoded_frames);
  548. if (!buffer)
  549. goto done;
  550. }
  551. ctx->eos_received |= !!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS);
  552. if (ctx->eos_received)
  553. goto done;
  554. if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED) {
  555. MMAL_COMPONENT_T *decoder = ctx->decoder;
  556. MMAL_EVENT_FORMAT_CHANGED_T *ev = mmal_event_format_changed_get(buffer);
  557. MMAL_BUFFER_HEADER_T *stale_buffer;
  558. av_log(avctx, AV_LOG_INFO, "Changing output format.\n");
  559. if ((status = mmal_port_disable(decoder->output[0])))
  560. goto done;
  561. while ((stale_buffer = mmal_queue_get(ctx->queue_decoded_frames)))
  562. mmal_buffer_header_release(stale_buffer);
  563. mmal_format_copy(decoder->output[0]->format, ev->format);
  564. if ((ret = ffmal_update_format(avctx)) < 0)
  565. goto done;
  566. if ((status = mmal_port_enable(decoder->output[0], output_callback)))
  567. goto done;
  568. if ((ret = ffmmal_fill_output_port(avctx)) < 0)
  569. goto done;
  570. if ((ret = ffmmal_fill_input_port(avctx)) < 0)
  571. goto done;
  572. mmal_buffer_header_release(buffer);
  573. continue;
  574. } else if (buffer->cmd) {
  575. char s[20];
  576. av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
  577. av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on output port\n", s);
  578. goto done;
  579. } else if (buffer->length == 0) {
  580. // Unused output buffer that got drained after format change.
  581. mmal_buffer_header_release(buffer);
  582. continue;
  583. }
  584. ctx->frames_output++;
  585. if ((ret = ffmal_copy_frame(avctx, frame, buffer)) < 0)
  586. goto done;
  587. *got_frame = 1;
  588. break;
  589. }
  590. done:
  591. if (buffer)
  592. mmal_buffer_header_release(buffer);
  593. if (status && ret >= 0)
  594. ret = AVERROR_UNKNOWN;
  595. return ret;
  596. }
  597. static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame,
  598. AVPacket *avpkt)
  599. {
  600. AVFrame *frame = data;
  601. int ret = 0;
  602. if ((ret = ffmmal_add_packet(avctx, avpkt)) < 0)
  603. return ret;
  604. if ((ret = ffmmal_fill_input_port(avctx)) < 0)
  605. return ret;
  606. if ((ret = ffmmal_fill_output_port(avctx)) < 0)
  607. return ret;
  608. if ((ret = ffmmal_read_frame(avctx, frame, got_frame)) < 0)
  609. return ret;
  610. // ffmmal_read_frame() can block for a while. Since the decoder is
  611. // asynchronous, it's a good idea to fill the ports again.
  612. if ((ret = ffmmal_fill_output_port(avctx)) < 0)
  613. return ret;
  614. if ((ret = ffmmal_fill_input_port(avctx)) < 0)
  615. return ret;
  616. return ret;
  617. }
  618. AVHWAccel ff_h264_mmal_hwaccel = {
  619. .name = "h264_mmal",
  620. .type = AVMEDIA_TYPE_VIDEO,
  621. .id = AV_CODEC_ID_H264,
  622. .pix_fmt = AV_PIX_FMT_MMAL,
  623. };
  624. static const AVOption options[]={
  625. {"extra_buffers", "extra buffers", offsetof(MMALDecodeContext, extra_buffers), AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
  626. {NULL}
  627. };
  628. static const AVClass ffmmaldec_class = {
  629. .class_name = "mmaldec",
  630. .option = options,
  631. .version = LIBAVUTIL_VERSION_INT,
  632. };
  633. AVCodec ff_h264_mmal_decoder = {
  634. .name = "h264_mmal",
  635. .long_name = NULL_IF_CONFIG_SMALL("h264 (mmal)"),
  636. .type = AVMEDIA_TYPE_VIDEO,
  637. .id = AV_CODEC_ID_H264,
  638. .priv_data_size = sizeof(MMALDecodeContext),
  639. .init = ffmmal_init_decoder,
  640. .close = ffmmal_close_decoder,
  641. .decode = ffmmal_decode,
  642. .flush = ffmmal_flush,
  643. .priv_class = &ffmmaldec_class,
  644. .capabilities = AV_CODEC_CAP_DELAY,
  645. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MMAL,
  646. AV_PIX_FMT_YUV420P,
  647. AV_PIX_FMT_NONE},
  648. };