1/* 2 * Copyright (c) 2010, Google, Inc. 3 * 4 * This file is part of FFmpeg. 5 * 6 * FFmpeg is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * FFmpeg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with FFmpeg; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21/** 22 * @file 23 * VP8 decoder support via libvpx 24 */ 25 26#define VPX_CODEC_DISABLE_COMPAT 1 27#include <vpx/vpx_decoder.h> 28#include <vpx/vp8dx.h> 29 30#include "avcodec.h" 31 32typedef struct VP8DecoderContext { 33 struct vpx_codec_ctx decoder; 34} VP8Context; 35 36static av_cold int vp8_init(AVCodecContext *avctx) 37{ 38 VP8Context *ctx = avctx->priv_data; 39 const struct vpx_codec_iface *iface = &vpx_codec_vp8_dx_algo; 40 struct vpx_codec_dec_cfg deccfg = { 41 /* token partitions+1 would be a decent choice */ 42 .threads = FFMIN(avctx->thread_count, 16) 43 }; 44 45 av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str()); 46 av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config()); 47 48 if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) { 49 const char *error = vpx_codec_error(&ctx->decoder); 50 av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n", 51 error); 52 return AVERROR(EINVAL); 53 } 54 55 avctx->pix_fmt = PIX_FMT_YUV420P; 56 return 0; 57} 58 59static int vp8_decode(AVCodecContext *avctx, 60 void *data, int *data_size, AVPacket *avpkt) 61{ 62 VP8Context *ctx = avctx->priv_data; 63 AVFrame *picture = data; 64 const void *iter = NULL; 65 struct vpx_image *img; 66 67 if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) != 68 VPX_CODEC_OK) { 69 const char *error = vpx_codec_error(&ctx->decoder); 70 const char *detail = vpx_codec_error_detail(&ctx->decoder); 71 72 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error); 73 if (detail) 74 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", 75 detail); 76 return AVERROR_INVALIDDATA; 77 } 78 79 if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) { 80 if (img->fmt != VPX_IMG_FMT_I420) { 81 av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n", 82 img->fmt); 83 return AVERROR_INVALIDDATA; 84 } 85 86 if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) { 87 av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n", 88 avctx->width, avctx->height, img->d_w, img->d_h); 89 if (avcodec_check_dimensions(avctx, img->d_w, img->d_h)) 90 return AVERROR_INVALIDDATA; 91 avcodec_set_dimensions(avctx, img->d_w, img->d_h); 92 } 93 picture->data[0] = img->planes[0]; 94 picture->data[1] = img->planes[1]; 95 picture->data[2] = img->planes[2]; 96 picture->data[3] = NULL; 97 picture->linesize[0] = img->stride[0]; 98 picture->linesize[1] = img->stride[1]; 99 picture->linesize[2] = img->stride[2]; 100 picture->linesize[3] = 0; 101 *data_size = sizeof(AVPicture); 102 } 103 return avpkt->size; 104} 105 106static av_cold int vp8_free(AVCodecContext *avctx) 107{ 108 VP8Context *ctx = avctx->priv_data; 109 vpx_codec_destroy(&ctx->decoder); 110 return 0; 111} 112 113AVCodec libvpx_decoder = { 114 "libvpx", 115 AVMEDIA_TYPE_VIDEO, 116 CODEC_ID_VP8, 117 sizeof(VP8Context), 118 vp8_init, 119 NULL, /* encode */ 120 vp8_free, 121 vp8_decode, 122 0, /* capabilities */ 123 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"), 124}; 125