yading@10: /* yading@10: * Copyright (c) 2010, Google, Inc. yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * VP8 decoder support via libvpx yading@10: */ yading@10: yading@10: #define VPX_CODEC_DISABLE_COMPAT 1 yading@10: #include yading@10: #include yading@10: yading@10: #include "libavutil/common.h" yading@10: #include "libavutil/imgutils.h" yading@10: #include "avcodec.h" yading@10: #include "internal.h" yading@10: yading@10: typedef struct VP8DecoderContext { yading@10: struct vpx_codec_ctx decoder; yading@10: } VP8Context; yading@10: yading@10: static av_cold int vpx_init(AVCodecContext *avctx, yading@10: const struct vpx_codec_iface *iface) yading@10: { yading@10: VP8Context *ctx = avctx->priv_data; yading@10: struct vpx_codec_dec_cfg deccfg = { yading@10: /* token partitions+1 would be a decent choice */ yading@10: .threads = FFMIN(avctx->thread_count, 16) yading@10: }; yading@10: yading@10: av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str()); yading@10: av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config()); yading@10: yading@10: if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) { yading@10: const char *error = vpx_codec_error(&ctx->decoder); yading@10: av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n", yading@10: error); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: avctx->pix_fmt = AV_PIX_FMT_YUV420P; yading@10: return 0; yading@10: } yading@10: yading@10: static int vp8_decode(AVCodecContext *avctx, yading@10: void *data, int *got_frame, AVPacket *avpkt) yading@10: { yading@10: VP8Context *ctx = avctx->priv_data; yading@10: AVFrame *picture = data; yading@10: const void *iter = NULL; yading@10: struct vpx_image *img; yading@10: int ret; yading@10: yading@10: if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) != yading@10: VPX_CODEC_OK) { yading@10: const char *error = vpx_codec_error(&ctx->decoder); yading@10: const char *detail = vpx_codec_error_detail(&ctx->decoder); yading@10: yading@10: av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error); yading@10: if (detail) yading@10: av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", yading@10: detail); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: yading@10: if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) { yading@10: if (img->fmt != VPX_IMG_FMT_I420) { yading@10: av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n", yading@10: img->fmt); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: yading@10: if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) { yading@10: av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n", yading@10: avctx->width, avctx->height, img->d_w, img->d_h); yading@10: if (av_image_check_size(img->d_w, img->d_h, 0, avctx)) yading@10: return AVERROR_INVALIDDATA; yading@10: avcodec_set_dimensions(avctx, img->d_w, img->d_h); yading@10: } yading@10: if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) yading@10: return ret; yading@10: av_image_copy(picture->data, picture->linesize, img->planes, yading@10: img->stride, avctx->pix_fmt, img->d_w, img->d_h); yading@10: *got_frame = 1; yading@10: } yading@10: return avpkt->size; yading@10: } yading@10: yading@10: static av_cold int vp8_free(AVCodecContext *avctx) yading@10: { yading@10: VP8Context *ctx = avctx->priv_data; yading@10: vpx_codec_destroy(&ctx->decoder); yading@10: return 0; yading@10: } yading@10: yading@10: #if CONFIG_LIBVPX_VP8_DECODER yading@10: static av_cold int vp8_init(AVCodecContext *avctx) yading@10: { yading@10: return vpx_init(avctx, &vpx_codec_vp8_dx_algo); yading@10: } yading@10: yading@10: AVCodec ff_libvpx_vp8_decoder = { yading@10: .name = "libvpx", yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: .id = AV_CODEC_ID_VP8, yading@10: .priv_data_size = sizeof(VP8Context), yading@10: .init = vp8_init, yading@10: .close = vp8_free, yading@10: .decode = vp8_decode, yading@10: .capabilities = CODEC_CAP_AUTO_THREADS | CODEC_CAP_DR1, yading@10: .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"), yading@10: }; yading@10: #endif /* CONFIG_LIBVPX_VP8_DECODER */ yading@10: yading@10: #if CONFIG_LIBVPX_VP9_DECODER yading@10: static av_cold int vp9_init(AVCodecContext *avctx) yading@10: { yading@10: return vpx_init(avctx, &vpx_codec_vp9_dx_algo); yading@10: } yading@10: yading@10: AVCodec ff_libvpx_vp9_decoder = { yading@10: .name = "libvpx-vp9", yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: .id = AV_CODEC_ID_VP9, yading@10: .priv_data_size = sizeof(VP8Context), yading@10: .init = vp9_init, yading@10: .close = vp8_free, yading@10: .decode = vp8_decode, yading@10: .capabilities = CODEC_CAP_AUTO_THREADS | CODEC_CAP_EXPERIMENTAL, yading@10: .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"), yading@10: }; yading@10: #endif /* CONFIG_LIBVPX_VP9_DECODER */