You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
5.9KB

  1. /*
  2. * V210 decoder
  3. *
  4. * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
  5. * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "avcodec.h"
  24. #include "internal.h"
  25. #include "v210dec.h"
  26. #include "libavutil/bswap.h"
  27. #include "libavutil/internal.h"
  28. #include "libavutil/mem.h"
  29. #include "libavutil/intreadwrite.h"
  30. #define READ_PIXELS(a, b, c) \
  31. do { \
  32. val = av_le2ne32(*src++); \
  33. *a++ = val & 0x3FF; \
  34. *b++ = (val >> 10) & 0x3FF; \
  35. *c++ = (val >> 20) & 0x3FF; \
  36. } while (0)
  37. static void v210_planar_unpack_c(const uint32_t *src, uint16_t *y, uint16_t *u, uint16_t *v, int width)
  38. {
  39. uint32_t val;
  40. int i;
  41. for( i = 0; i < width-5; i += 6 ){
  42. READ_PIXELS(u, y, v);
  43. READ_PIXELS(y, u, y);
  44. READ_PIXELS(v, y, u);
  45. READ_PIXELS(y, v, y);
  46. }
  47. }
  48. av_cold void ff_v210dec_init(V210DecContext *s)
  49. {
  50. s->unpack_frame = v210_planar_unpack_c;
  51. if (ARCH_X86)
  52. ff_v210_x86_init(s);
  53. }
  54. static av_cold int decode_init(AVCodecContext *avctx)
  55. {
  56. V210DecContext *s = avctx->priv_data;
  57. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  58. avctx->bits_per_raw_sample = 10;
  59. s->aligned_input = 0;
  60. ff_v210dec_init(s);
  61. return 0;
  62. }
  63. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  64. AVPacket *avpkt)
  65. {
  66. V210DecContext *s = avctx->priv_data;
  67. int h, w, ret, stride, aligned_input;
  68. AVFrame *pic = data;
  69. const uint8_t *psrc = avpkt->data;
  70. uint16_t *y, *u, *v;
  71. if (s->custom_stride )
  72. stride = s->custom_stride;
  73. else {
  74. int aligned_width = ((avctx->width + 47) / 48) * 48;
  75. stride = aligned_width * 8 / 3;
  76. }
  77. if (avpkt->size < stride * avctx->height) {
  78. if ((((avctx->width + 23) / 24) * 24 * 8) / 3 * avctx->height == avpkt->size) {
  79. stride = avpkt->size / avctx->height;
  80. if (!s->stride_warning_shown)
  81. av_log(avctx, AV_LOG_WARNING, "Broken v210 with too small padding (64 byte) detected\n");
  82. s->stride_warning_shown = 1;
  83. } else {
  84. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  85. return AVERROR_INVALIDDATA;
  86. }
  87. }
  88. if ( avctx->codec_tag == MKTAG('C', '2', '1', '0')
  89. && avpkt->size > 64
  90. && AV_RN32(psrc) == AV_RN32("INFO")
  91. && avpkt->size - 64 >= stride * avctx->height)
  92. psrc += 64;
  93. aligned_input = !((uintptr_t)psrc & 0x1f) && !(stride & 0x1f);
  94. if (aligned_input != s->aligned_input) {
  95. s->aligned_input = aligned_input;
  96. ff_v210dec_init(s);
  97. }
  98. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  99. return ret;
  100. y = (uint16_t*)pic->data[0];
  101. u = (uint16_t*)pic->data[1];
  102. v = (uint16_t*)pic->data[2];
  103. pic->pict_type = AV_PICTURE_TYPE_I;
  104. pic->key_frame = 1;
  105. for (h = 0; h < avctx->height; h++) {
  106. const uint32_t *src = (const uint32_t*)psrc;
  107. uint32_t val;
  108. w = (avctx->width / 12) * 12;
  109. s->unpack_frame(src, y, u, v, w);
  110. y += w;
  111. u += w >> 1;
  112. v += w >> 1;
  113. src += (w << 1) / 3;
  114. if (w < avctx->width - 5) {
  115. READ_PIXELS(u, y, v);
  116. READ_PIXELS(y, u, y);
  117. READ_PIXELS(v, y, u);
  118. READ_PIXELS(y, v, y);
  119. w += 6;
  120. }
  121. if (w < avctx->width - 1) {
  122. READ_PIXELS(u, y, v);
  123. val = av_le2ne32(*src++);
  124. *y++ = val & 0x3FF;
  125. if (w < avctx->width - 3) {
  126. *u++ = (val >> 10) & 0x3FF;
  127. *y++ = (val >> 20) & 0x3FF;
  128. val = av_le2ne32(*src++);
  129. *v++ = val & 0x3FF;
  130. *y++ = (val >> 10) & 0x3FF;
  131. }
  132. }
  133. psrc += stride;
  134. y += pic->linesize[0] / 2 - avctx->width + (avctx->width & 1);
  135. u += pic->linesize[1] / 2 - avctx->width / 2;
  136. v += pic->linesize[2] / 2 - avctx->width / 2;
  137. }
  138. if (avctx->field_order > AV_FIELD_PROGRESSIVE) {
  139. /* we have interlaced material flagged in container */
  140. pic->interlaced_frame = 1;
  141. if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
  142. pic->top_field_first = 1;
  143. }
  144. *got_frame = 1;
  145. return avpkt->size;
  146. }
  147. #define V210DEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  148. static const AVOption v210dec_options[] = {
  149. {"custom_stride", "Custom V210 stride", offsetof(V210DecContext, custom_stride), AV_OPT_TYPE_INT,
  150. {.i64 = 0}, INT_MIN, INT_MAX, V210DEC_FLAGS},
  151. {NULL}
  152. };
  153. static const AVClass v210dec_class = {
  154. .class_name = "V210 Decoder",
  155. .item_name = av_default_item_name,
  156. .option = v210dec_options,
  157. .version = LIBAVUTIL_VERSION_INT,
  158. };
  159. AVCodec ff_v210_decoder = {
  160. .name = "v210",
  161. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  162. .type = AVMEDIA_TYPE_VIDEO,
  163. .id = AV_CODEC_ID_V210,
  164. .priv_data_size = sizeof(V210DecContext),
  165. .init = decode_init,
  166. .decode = decode_frame,
  167. .capabilities = AV_CODEC_CAP_DR1,
  168. .priv_class = &v210dec_class,
  169. };