00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "avcodec.h"
00025 #include "v210dec.h"
00026 #include "libavutil/bswap.h"
00027 #include "libavutil/internal.h"
00028 #include "libavutil/mem.h"
00029
00030 #define READ_PIXELS(a, b, c) \
00031 do { \
00032 val = av_le2ne32(*src++); \
00033 *a++ = val & 0x3FF; \
00034 *b++ = (val >> 10) & 0x3FF; \
00035 *c++ = (val >> 20) & 0x3FF; \
00036 } while (0)
00037
00038 static void v210_planar_unpack_c(const uint32_t *src, uint16_t *y, uint16_t *u, uint16_t *v, int width)
00039 {
00040 uint32_t val;
00041 int i;
00042
00043 for( i = 0; i < width-5; i += 6 ){
00044 READ_PIXELS(u, y, v);
00045 READ_PIXELS(y, u, y);
00046 READ_PIXELS(v, y, u);
00047 READ_PIXELS(y, v, y);
00048 }
00049 }
00050
00051 static av_cold int decode_init(AVCodecContext *avctx)
00052 {
00053 V210DecContext *s = avctx->priv_data;
00054
00055 if (avctx->width & 1) {
00056 av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
00057 return -1;
00058 }
00059 avctx->pix_fmt = PIX_FMT_YUV422P10;
00060 avctx->bits_per_raw_sample = 10;
00061
00062 avctx->coded_frame = avcodec_alloc_frame();
00063 if (!avctx->coded_frame)
00064 return AVERROR(ENOMEM);
00065
00066 s->unpack_frame = v210_planar_unpack_c;
00067
00068 if (HAVE_MMX)
00069 v210_x86_init(s);
00070
00071 return 0;
00072 }
00073
00074 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00075 AVPacket *avpkt)
00076 {
00077 V210DecContext *s = avctx->priv_data;
00078
00079 int h, w, stride, aligned_input;
00080 AVFrame *pic = avctx->coded_frame;
00081 const uint8_t *psrc = avpkt->data;
00082 uint16_t *y, *u, *v;
00083
00084 if (s->custom_stride )
00085 stride = s->custom_stride;
00086 else {
00087 int aligned_width = ((avctx->width + 47) / 48) * 48;
00088 stride = aligned_width * 8 / 3;
00089 }
00090
00091 if (avpkt->size < stride * avctx->height) {
00092 if ((((avctx->width + 23) / 24) * 24 * 8) / 3 * avctx->height == avpkt->size) {
00093 stride = avpkt->size / avctx->height;
00094 if (!s->stride_warning_shown)
00095 av_log(avctx, AV_LOG_WARNING, "Broken v210 with too small padding (64 byte) detected\n");
00096 s->stride_warning_shown = 1;
00097 } else {
00098 av_log(avctx, AV_LOG_ERROR, "packet too small\n");
00099 return -1;
00100 }
00101 }
00102
00103 aligned_input = !((uintptr_t)psrc & 0xf) && !(stride & 0xf);
00104 if (aligned_input != s->aligned_input) {
00105 s->aligned_input = aligned_input;
00106 if (HAVE_MMX)
00107 v210_x86_init(s);
00108 }
00109
00110 if (pic->data[0])
00111 avctx->release_buffer(avctx, pic);
00112
00113 pic->reference = 0;
00114 if (avctx->get_buffer(avctx, pic) < 0)
00115 return -1;
00116
00117 y = (uint16_t*)pic->data[0];
00118 u = (uint16_t*)pic->data[1];
00119 v = (uint16_t*)pic->data[2];
00120 pic->pict_type = AV_PICTURE_TYPE_I;
00121 pic->key_frame = 1;
00122
00123 for (h = 0; h < avctx->height; h++) {
00124 const uint32_t *src = (const uint32_t*)psrc;
00125 uint32_t val;
00126
00127 w = (avctx->width / 6) * 6;
00128 s->unpack_frame(src, y, u, v, w);
00129
00130 y += w;
00131 u += w >> 1;
00132 v += w >> 1;
00133 src += (w << 1) / 3;
00134
00135 if (w < avctx->width - 1) {
00136 READ_PIXELS(u, y, v);
00137
00138 val = av_le2ne32(*src++);
00139 *y++ = val & 0x3FF;
00140 if (w < avctx->width - 3) {
00141 *u++ = (val >> 10) & 0x3FF;
00142 *y++ = (val >> 20) & 0x3FF;
00143
00144 val = av_le2ne32(*src++);
00145 *v++ = val & 0x3FF;
00146 *y++ = (val >> 10) & 0x3FF;
00147 }
00148 }
00149
00150 psrc += stride;
00151 y += pic->linesize[0] / 2 - avctx->width;
00152 u += pic->linesize[1] / 2 - avctx->width / 2;
00153 v += pic->linesize[2] / 2 - avctx->width / 2;
00154 }
00155
00156 *data_size = sizeof(AVFrame);
00157 *(AVFrame*)data = *avctx->coded_frame;
00158
00159 return avpkt->size;
00160 }
00161
00162 static av_cold int decode_close(AVCodecContext *avctx)
00163 {
00164 AVFrame *pic = avctx->coded_frame;
00165 if (pic->data[0])
00166 avctx->release_buffer(avctx, pic);
00167 av_freep(&avctx->coded_frame);
00168
00169 return 0;
00170 }
00171
00172 #define V210DEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
00173 static const AVOption v210dec_options[] = {
00174 {"custom_stride", "Custom V210 stride", offsetof(V210DecContext, custom_stride), FF_OPT_TYPE_INT,
00175 {.dbl = 0}, INT_MIN, INT_MAX, V210DEC_FLAGS},
00176 {NULL}
00177 };
00178
00179 static const AVClass v210dec_class = {
00180 "V210 Decoder",
00181 av_default_item_name,
00182 v210dec_options,
00183 LIBAVUTIL_VERSION_INT,
00184 };
00185
00186 AVCodec ff_v210_decoder = {
00187 .name = "v210",
00188 .type = AVMEDIA_TYPE_VIDEO,
00189 .id = AV_CODEC_ID_V210,
00190 .priv_data_size = sizeof(V210DecContext),
00191 .init = decode_init,
00192 .close = decode_close,
00193 .decode = decode_frame,
00194 .capabilities = CODEC_CAP_DR1,
00195 .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
00196 .priv_class = &v210dec_class,
00197 };