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.

784 lines
24KB

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