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