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.

151 lines
4.2KB

  1. /*
  2. * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
  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. #include "avcodec.h"
  21. #include "internal.h"
  22. #include "libavutil/bswap.h"
  23. #include "libavutil/internal.h"
  24. #include "libavutil/mem.h"
  25. static av_cold int decode_init(AVCodecContext *avctx)
  26. {
  27. if(avctx->width & 1){
  28. av_log(avctx, AV_LOG_ERROR, "v210x needs even width\n");
  29. return -1;
  30. }
  31. avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
  32. avctx->bits_per_raw_sample= 10;
  33. avctx->coded_frame= avcodec_alloc_frame();
  34. if (!avctx->coded_frame)
  35. return AVERROR(ENOMEM);
  36. return 0;
  37. }
  38. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  39. AVPacket *avpkt)
  40. {
  41. int y=0;
  42. int width= avctx->width;
  43. AVFrame *pic= avctx->coded_frame;
  44. const uint32_t *src= (const uint32_t *)avpkt->data;
  45. uint16_t *ydst, *udst, *vdst, *yend;
  46. if(pic->data[0])
  47. avctx->release_buffer(avctx, pic);
  48. if(avpkt->size < avctx->width * avctx->height * 8 / 3){
  49. av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
  50. return -1;
  51. }
  52. if(avpkt->size > avctx->width * avctx->height * 8 / 3){
  53. av_log_ask_for_sample(avctx, "Probably padded data\n");
  54. }
  55. pic->reference= 0;
  56. if(ff_get_buffer(avctx, pic) < 0)
  57. return -1;
  58. ydst= (uint16_t *)pic->data[0];
  59. udst= (uint16_t *)pic->data[1];
  60. vdst= (uint16_t *)pic->data[2];
  61. yend= ydst + width;
  62. pic->pict_type= AV_PICTURE_TYPE_I;
  63. pic->key_frame= 1;
  64. for(;;){
  65. uint32_t v= av_be2ne32(*src++);
  66. *udst++= (v>>16) & 0xFFC0;
  67. *ydst++= (v>>6 ) & 0xFFC0;
  68. *vdst++= (v<<4 ) & 0xFFC0;
  69. v= av_be2ne32(*src++);
  70. *ydst++= (v>>16) & 0xFFC0;
  71. if(ydst >= yend){
  72. ydst+= pic->linesize[0]/2 - width;
  73. udst+= pic->linesize[1]/2 - width/2;
  74. vdst+= pic->linesize[2]/2 - width/2;
  75. yend= ydst + width;
  76. if(++y >= avctx->height)
  77. break;
  78. }
  79. *udst++= (v>>6 ) & 0xFFC0;
  80. *ydst++= (v<<4 ) & 0xFFC0;
  81. v= av_be2ne32(*src++);
  82. *vdst++= (v>>16) & 0xFFC0;
  83. *ydst++= (v>>6 ) & 0xFFC0;
  84. if(ydst >= yend){
  85. ydst+= pic->linesize[0]/2 - width;
  86. udst+= pic->linesize[1]/2 - width/2;
  87. vdst+= pic->linesize[2]/2 - width/2;
  88. yend= ydst + width;
  89. if(++y >= avctx->height)
  90. break;
  91. }
  92. *udst++= (v<<4 ) & 0xFFC0;
  93. v= av_be2ne32(*src++);
  94. *ydst++= (v>>16) & 0xFFC0;
  95. *vdst++= (v>>6 ) & 0xFFC0;
  96. *ydst++= (v<<4 ) & 0xFFC0;
  97. if(ydst >= yend){
  98. ydst+= pic->linesize[0]/2 - width;
  99. udst+= pic->linesize[1]/2 - width/2;
  100. vdst+= pic->linesize[2]/2 - width/2;
  101. yend= ydst + width;
  102. if(++y >= avctx->height)
  103. break;
  104. }
  105. }
  106. *got_frame = 1;
  107. *(AVFrame*)data= *avctx->coded_frame;
  108. return avpkt->size;
  109. }
  110. static av_cold int decode_close(AVCodecContext *avctx)
  111. {
  112. AVFrame *pic = avctx->coded_frame;
  113. if (pic->data[0])
  114. avctx->release_buffer(avctx, pic);
  115. av_freep(&avctx->coded_frame);
  116. return 0;
  117. }
  118. AVCodec ff_v210x_decoder = {
  119. .name = "v210x",
  120. .type = AVMEDIA_TYPE_VIDEO,
  121. .id = AV_CODEC_ID_V210X,
  122. .init = decode_init,
  123. .close = decode_close,
  124. .decode = decode_frame,
  125. .capabilities = CODEC_CAP_DR1,
  126. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  127. };