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.

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