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.

797 lines
25KB

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