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.

785 lines
24KB

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