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.

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