[FFmpeg-devel] [PATCH] Add android_capture indev
Michael Niedermayer
michael at niedermayer.cc
Thu Dec 28 20:20:19 EET 2017
On Fri, Dec 22, 2017 at 09:36:59PM +0100, Felix Matouschek wrote:
> Am 22.12.2017 20:50, schrieb Lou Logan:
> >
> >I think you forgot to attach the patch.
>
> Sorry, flaky mail client... attached it again.
[...]
> +static void image_available(void *context, AImageReader *reader)
> +{
> + AVFormatContext *avctx = context;
> + AndroidCameraCtx *ctx = avctx->priv_data;
> + media_status_t media_status;
> + int ret = 0;
> +
> + AImage *image;
> + int64_t image_timestamp;
> + int32_t image_linestrides[4];
> + uint8_t *image_plane_data[4];
> + int plane_data_length[4];
> +
> + AVPacket pkt;
> + int pkt_buffer_size = 0;
> +
> + media_status = AImageReader_acquireLatestImage(reader, &image);
> + if (media_status != AMEDIA_OK) {
> + if (media_status == AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE) {
> + av_log(avctx, AV_LOG_WARNING,
> + "An image reader frame was discarded");
> + } else {
> + av_log(avctx, AV_LOG_ERROR,
> + "Failed to acquire latest image from image reader, error: %s.\n",
> + media_status_string(media_status));
> + ret = AVERROR_EXTERNAL;
> + }
> + goto error;
> + }
> +
> + // Silently drop frames when exit is set
> + if (atomic_load(&ctx->exit)) {
> + goto error;
> + }
> +
> + // Determine actual image format
> + if (!atomic_load(&ctx->got_image_format)) {
> + ret = get_image_format(avctx, image);
> + if (ret < 0) {
> + av_log(avctx, AV_LOG_ERROR,
> + "Could not get image format of camera.\n");
> + goto error;
> + } else {
> + atomic_store(&ctx->got_image_format, 1);
> + }
> + }
> +
> + pkt_buffer_size = av_image_get_buffer_size(ctx->image_format, ctx->width, ctx->height, 32);
> + AImage_getTimestamp(image, &image_timestamp);
> +
> + AImage_getPlaneRowStride(image, 0, &image_linestrides[0]);
> + AImage_getPlaneData(image, 0, &image_plane_data[0], &plane_data_length[0]);
> +
> + switch (ctx->image_format) {
> + case AV_PIX_FMT_YUV420P:
> + AImage_getPlaneRowStride(image, 1, &image_linestrides[1]);
> + AImage_getPlaneData(image, 1, &image_plane_data[1], &plane_data_length[1]);
> + AImage_getPlaneRowStride(image, 2, &image_linestrides[2]);
> + AImage_getPlaneData(image, 2, &image_plane_data[2], &plane_data_length[2]);
> + break;
> + case AV_PIX_FMT_NV12:
> + AImage_getPlaneRowStride(image, 1, &image_linestrides[1]);
> + AImage_getPlaneData(image, 1, &image_plane_data[1], &plane_data_length[1]);
> + break;
> + case AV_PIX_FMT_NV21:
> + AImage_getPlaneRowStride(image, 2, &image_linestrides[1]);
> + AImage_getPlaneData(image, 2, &image_plane_data[1], &plane_data_length[1]);
> + break;
> + default:
> + av_log(avctx, AV_LOG_ERROR, "Unsupported camera image format.\n");
> + ret = AVERROR(ENOSYS);
> + goto error;
> + }
> +
> + ret = av_new_packet(&pkt, pkt_buffer_size);
> + if (ret < 0) {
> + av_log(avctx, AV_LOG_ERROR,
> + "Failed to create new av packet, error: %s.\n", av_err2str(ret));
> + goto error;
> + }
> +
> + pkt.stream_index = VIDEO_STREAM_INDEX;
> + pkt.pts = image_timestamp;
> + av_image_copy_to_buffer(pkt.data, pkt_buffer_size,
> + (const uint8_t * const *) image_plane_data,
> + image_linestrides, ctx->image_format,
> + ctx->width, ctx->height, 32);
Is the copy needed ?
can the data not be put in a AVPacket without copy but by pointing to the image?
the AVPackets deallocation can be overridden to free the image
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Never trust a computer, one day, it may think you are the virus. -- Compn
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20171228/7019db46/attachment.sig>
More information about the ffmpeg-devel
mailing list