v210x.c 3.9 KB

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