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