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.

852 lines
27KB

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