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.

829 lines
26KB

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