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.

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