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.

781 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. } else {
  356. buf = av_buffer_alloc(avpkt->size);
  357. if (buf)
  358. memcpy(buf->data, avpkt->data, avpkt->size);
  359. }
  360. if (!buf) {
  361. ret = AVERROR(ENOMEM);
  362. goto done;
  363. }
  364. size = buf->size;
  365. data = buf->data;
  366. if (!is_extradata)
  367. ctx->packets_sent++;
  368. } else {
  369. if (!ctx->packets_sent) {
  370. // Short-cut the flush logic to avoid upsetting MMAL.
  371. ctx->eos_sent = 1;
  372. ctx->eos_received = 1;
  373. goto done;
  374. }
  375. }
  376. start = data;
  377. do {
  378. FFBufferEntry *buffer = av_mallocz(sizeof(*buffer));
  379. if (!buffer) {
  380. ret = AVERROR(ENOMEM);
  381. goto done;
  382. }
  383. buffer->data = data;
  384. buffer->length = FFMIN(size, ctx->decoder->input[0]->buffer_size);
  385. if (is_extradata)
  386. buffer->flags |= MMAL_BUFFER_HEADER_FLAG_CONFIG;
  387. if (data == start)
  388. buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_START;
  389. data += buffer->length;
  390. size -= buffer->length;
  391. buffer->pts = avpkt->pts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->pts;
  392. buffer->dts = avpkt->dts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->dts;
  393. if (!size)
  394. buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
  395. if (!buffer->length) {
  396. buffer->flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
  397. ctx->eos_sent = 1;
  398. }
  399. if (buf) {
  400. buffer->ref = av_buffer_ref(buf);
  401. if (!buffer->ref) {
  402. av_free(buffer);
  403. ret = AVERROR(ENOMEM);
  404. goto done;
  405. }
  406. }
  407. // Insert at end of the list
  408. if (!ctx->waiting_buffers)
  409. ctx->waiting_buffers = buffer;
  410. if (ctx->waiting_buffers_tail)
  411. ctx->waiting_buffers_tail->next = buffer;
  412. ctx->waiting_buffers_tail = buffer;
  413. } while (size);
  414. done:
  415. av_buffer_unref(&buf);
  416. return ret;
  417. }
  418. // Move prepared/split packets from waiting_buffers to the MMAL decoder.
  419. static int ffmmal_fill_input_port(AVCodecContext *avctx)
  420. {
  421. MMALDecodeContext *ctx = avctx->priv_data;
  422. while (ctx->waiting_buffers) {
  423. MMAL_BUFFER_HEADER_T *mbuffer;
  424. FFBufferEntry *buffer;
  425. MMAL_STATUS_T status;
  426. mbuffer = mmal_queue_get(ctx->pool_in->queue);
  427. if (!mbuffer)
  428. return 0;
  429. buffer = ctx->waiting_buffers;
  430. mmal_buffer_header_reset(mbuffer);
  431. mbuffer->cmd = 0;
  432. mbuffer->pts = buffer->pts;
  433. mbuffer->dts = buffer->dts;
  434. mbuffer->flags = buffer->flags;
  435. mbuffer->data = buffer->data;
  436. mbuffer->length = buffer->length;
  437. mbuffer->user_data = buffer->ref;
  438. mbuffer->alloc_size = ctx->decoder->input[0]->buffer_size;
  439. if ((status = mmal_port_send_buffer(ctx->decoder->input[0], mbuffer))) {
  440. mmal_buffer_header_release(mbuffer);
  441. av_buffer_unref(&buffer->ref);
  442. }
  443. // Remove from start of the list
  444. ctx->waiting_buffers = buffer->next;
  445. if (ctx->waiting_buffers_tail == buffer)
  446. ctx->waiting_buffers_tail = NULL;
  447. av_free(buffer);
  448. if (status) {
  449. av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending input\n", (int)status);
  450. return AVERROR_UNKNOWN;
  451. }
  452. }
  453. return 0;
  454. }
  455. static int ffmal_copy_frame(AVCodecContext *avctx, AVFrame *frame,
  456. MMAL_BUFFER_HEADER_T *buffer)
  457. {
  458. MMALDecodeContext *ctx = avctx->priv_data;
  459. int ret = 0;
  460. if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
  461. if (!ctx->pool_out)
  462. return AVERROR_UNKNOWN; // format change code failed with OOM previously
  463. if ((ret = ff_decode_frame_props(avctx, frame)) < 0)
  464. goto done;
  465. if ((ret = ffmmal_set_ref(frame, ctx->pool_out, buffer)) < 0)
  466. goto done;
  467. } else {
  468. int w = FFALIGN(avctx->width, 32);
  469. int h = FFALIGN(avctx->height, 16);
  470. char *ptr;
  471. int plane;
  472. int i;
  473. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  474. goto done;
  475. ptr = buffer->data + buffer->type->video.offset[0];
  476. for (i = 0; i < avctx->height; i++)
  477. memcpy(frame->data[0] + frame->linesize[0] * i, ptr + w * i, avctx->width);
  478. ptr += w * h;
  479. for (plane = 1; plane < 3; plane++) {
  480. for (i = 0; i < avctx->height / 2; i++)
  481. memcpy(frame->data[plane] + frame->linesize[plane] * i, ptr + w / 2 * i, (avctx->width + 1) / 2);
  482. ptr += w / 2 * h / 2;
  483. }
  484. }
  485. frame->pkt_pts = buffer->pts == MMAL_TIME_UNKNOWN ? AV_NOPTS_VALUE : buffer->pts;
  486. frame->pkt_dts = AV_NOPTS_VALUE;
  487. done:
  488. return ret;
  489. }
  490. // Fetch a decoded buffer and place it into the frame parameter.
  491. static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
  492. {
  493. MMALDecodeContext *ctx = avctx->priv_data;
  494. MMAL_BUFFER_HEADER_T *buffer = NULL;
  495. MMAL_STATUS_T status = 0;
  496. int ret = 0;
  497. if (ctx->eos_received)
  498. goto done;
  499. while (1) {
  500. // To ensure decoding in lockstep with a constant delay between fed packets
  501. // and output frames, we always wait until an output buffer is available.
  502. // Except during start we don't know after how many input packets the decoder
  503. // is going to return the first buffer, and we can't distinguish decoder
  504. // being busy from decoder waiting for input. So just poll at the start and
  505. // keep feeding new data to the buffer.
  506. // We are pretty sure the decoder will produce output if we sent more input
  507. // frames than what a h264 decoder could logically delay. This avoids too
  508. // excessive buffering.
  509. // We also wait if we sent eos, but didn't receive it yet (think of decoding
  510. // stream with a very low number of frames).
  511. if (ctx->frames_output || ctx->packets_sent > MAX_DELAYED_FRAMES ||
  512. (ctx->packets_sent && ctx->eos_sent)) {
  513. // MMAL will ignore broken input packets, which means the frame we
  514. // expect here may never arrive. Dealing with this correctly is
  515. // complicated, so here's a hack to avoid that it freezes forever
  516. // in this unlikely situation.
  517. buffer = mmal_queue_timedwait(ctx->queue_decoded_frames, 100);
  518. if (!buffer) {
  519. av_log(avctx, AV_LOG_ERROR, "Did not get output frame from MMAL.\n");
  520. ret = AVERROR_UNKNOWN;
  521. goto done;
  522. }
  523. } else {
  524. buffer = mmal_queue_get(ctx->queue_decoded_frames);
  525. if (!buffer)
  526. goto done;
  527. }
  528. ctx->eos_received |= !!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS);
  529. if (ctx->eos_received)
  530. goto done;
  531. if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED) {
  532. MMAL_COMPONENT_T *decoder = ctx->decoder;
  533. MMAL_EVENT_FORMAT_CHANGED_T *ev = mmal_event_format_changed_get(buffer);
  534. MMAL_BUFFER_HEADER_T *stale_buffer;
  535. av_log(avctx, AV_LOG_INFO, "Changing output format.\n");
  536. if ((status = mmal_port_disable(decoder->output[0])))
  537. goto done;
  538. while ((stale_buffer = mmal_queue_get(ctx->queue_decoded_frames)))
  539. mmal_buffer_header_release(stale_buffer);
  540. mmal_format_copy(decoder->output[0]->format, ev->format);
  541. if ((ret = ffmal_update_format(avctx)) < 0)
  542. goto done;
  543. if ((status = mmal_port_enable(decoder->output[0], output_callback)))
  544. goto done;
  545. if ((ret = ffmmal_fill_output_port(avctx)) < 0)
  546. goto done;
  547. if ((ret = ffmmal_fill_input_port(avctx)) < 0)
  548. goto done;
  549. mmal_buffer_header_release(buffer);
  550. continue;
  551. } else if (buffer->cmd) {
  552. char s[20];
  553. av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
  554. av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on output port\n", s);
  555. goto done;
  556. } else if (buffer->length == 0) {
  557. // Unused output buffer that got drained after format change.
  558. mmal_buffer_header_release(buffer);
  559. continue;
  560. }
  561. ctx->frames_output++;
  562. if ((ret = ffmal_copy_frame(avctx, frame, buffer)) < 0)
  563. goto done;
  564. *got_frame = 1;
  565. break;
  566. }
  567. done:
  568. if (buffer)
  569. mmal_buffer_header_release(buffer);
  570. if (status && ret >= 0)
  571. ret = AVERROR_UNKNOWN;
  572. return ret;
  573. }
  574. static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame,
  575. AVPacket *avpkt)
  576. {
  577. MMALDecodeContext *ctx = avctx->priv_data;
  578. AVFrame *frame = data;
  579. int ret = 0;
  580. if (avctx->extradata_size && !ctx->extradata_sent) {
  581. AVPacket pkt = {0};
  582. av_init_packet(&pkt);
  583. pkt.data = avctx->extradata;
  584. pkt.size = avctx->extradata_size;
  585. ctx->extradata_sent = 1;
  586. if ((ret = ffmmal_add_packet(avctx, &pkt, 1)) < 0)
  587. return ret;
  588. }
  589. if ((ret = ffmmal_add_packet(avctx, avpkt, 0)) < 0)
  590. return ret;
  591. if ((ret = ffmmal_fill_input_port(avctx)) < 0)
  592. return ret;
  593. if ((ret = ffmmal_fill_output_port(avctx)) < 0)
  594. return ret;
  595. if ((ret = ffmmal_read_frame(avctx, frame, got_frame)) < 0)
  596. return ret;
  597. // ffmmal_read_frame() can block for a while. Since the decoder is
  598. // asynchronous, it's a good idea to fill the ports again.
  599. if ((ret = ffmmal_fill_output_port(avctx)) < 0)
  600. return ret;
  601. if ((ret = ffmmal_fill_input_port(avctx)) < 0)
  602. return ret;
  603. return ret;
  604. }
  605. AVHWAccel ff_h264_mmal_hwaccel = {
  606. .name = "h264_mmal",
  607. .type = AVMEDIA_TYPE_VIDEO,
  608. .id = AV_CODEC_ID_H264,
  609. .pix_fmt = AV_PIX_FMT_MMAL,
  610. };
  611. static const AVOption options[]={
  612. {"extra_buffers", "extra buffers", offsetof(MMALDecodeContext, extra_buffers), AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
  613. {NULL}
  614. };
  615. static const AVClass ffmmaldec_class = {
  616. .class_name = "mmaldec",
  617. .option = options,
  618. .version = LIBAVUTIL_VERSION_INT,
  619. };
  620. AVCodec ff_h264_mmal_decoder = {
  621. .name = "h264_mmal",
  622. .long_name = NULL_IF_CONFIG_SMALL("h264 (mmal)"),
  623. .type = AVMEDIA_TYPE_VIDEO,
  624. .id = AV_CODEC_ID_H264,
  625. .priv_data_size = sizeof(MMALDecodeContext),
  626. .init = ffmmal_init_decoder,
  627. .close = ffmmal_close_decoder,
  628. .decode = ffmmal_decode,
  629. .flush = ffmmal_flush,
  630. .priv_class = &ffmmaldec_class,
  631. .capabilities = AV_CODEC_CAP_DELAY,
  632. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  633. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MMAL,
  634. AV_PIX_FMT_YUV420P,
  635. AV_PIX_FMT_NONE},
  636. };