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.

208 lines
5.7KB

  1. /*
  2. * ATI VCR1 codec
  3. * Copyright (c) 2003 Michael Niedermayer
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * ati vcr1 codec.
  24. */
  25. #include "avcodec.h"
  26. #include "dsputil.h"
  27. //#undef NDEBUG
  28. //#include <assert.h>
  29. /* Disable the encoder. */
  30. #undef CONFIG_VCR1_ENCODER
  31. #define CONFIG_VCR1_ENCODER 0
  32. typedef struct VCR1Context{
  33. AVCodecContext *avctx;
  34. AVFrame picture;
  35. int delta[16];
  36. int offset[4];
  37. } VCR1Context;
  38. static int decode_frame(AVCodecContext *avctx,
  39. void *data, int *data_size,
  40. AVPacket *avpkt)
  41. {
  42. const uint8_t *buf = avpkt->data;
  43. int buf_size = avpkt->size;
  44. VCR1Context * const a = avctx->priv_data;
  45. AVFrame *picture = data;
  46. AVFrame * const p= (AVFrame*)&a->picture;
  47. const uint8_t *bytestream= buf;
  48. int i, x, y;
  49. if(p->data[0])
  50. avctx->release_buffer(avctx, p);
  51. if(buf_size < 16 + avctx->height + avctx->width*avctx->height*5/8){
  52. av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
  53. return AVERROR(EINVAL);
  54. }
  55. p->reference= 0;
  56. if(avctx->get_buffer(avctx, p) < 0){
  57. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  58. return -1;
  59. }
  60. p->pict_type= AV_PICTURE_TYPE_I;
  61. p->key_frame= 1;
  62. for(i=0; i<16; i++){
  63. a->delta[i]= *(bytestream++);
  64. bytestream++;
  65. }
  66. for(y=0; y<avctx->height; y++){
  67. int offset;
  68. uint8_t *luma= &a->picture.data[0][ y*a->picture.linesize[0] ];
  69. if((y&3) == 0){
  70. uint8_t *cb= &a->picture.data[1][ (y>>2)*a->picture.linesize[1] ];
  71. uint8_t *cr= &a->picture.data[2][ (y>>2)*a->picture.linesize[2] ];
  72. for(i=0; i<4; i++)
  73. a->offset[i]= *(bytestream++);
  74. offset= a->offset[0] - a->delta[ bytestream[2]&0xF ];
  75. for(x=0; x<avctx->width; x+=4){
  76. luma[0]=( offset += a->delta[ bytestream[2]&0xF ]);
  77. luma[1]=( offset += a->delta[ bytestream[2]>>4 ]);
  78. luma[2]=( offset += a->delta[ bytestream[0]&0xF ]);
  79. luma[3]=( offset += a->delta[ bytestream[0]>>4 ]);
  80. luma += 4;
  81. *(cb++) = bytestream[3];
  82. *(cr++) = bytestream[1];
  83. bytestream+= 4;
  84. }
  85. }else{
  86. offset= a->offset[y&3] - a->delta[ bytestream[2]&0xF ];
  87. for(x=0; x<avctx->width; x+=8){
  88. luma[0]=( offset += a->delta[ bytestream[2]&0xF ]);
  89. luma[1]=( offset += a->delta[ bytestream[2]>>4 ]);
  90. luma[2]=( offset += a->delta[ bytestream[3]&0xF ]);
  91. luma[3]=( offset += a->delta[ bytestream[3]>>4 ]);
  92. luma[4]=( offset += a->delta[ bytestream[0]&0xF ]);
  93. luma[5]=( offset += a->delta[ bytestream[0]>>4 ]);
  94. luma[6]=( offset += a->delta[ bytestream[1]&0xF ]);
  95. luma[7]=( offset += a->delta[ bytestream[1]>>4 ]);
  96. luma += 8;
  97. bytestream+= 4;
  98. }
  99. }
  100. }
  101. *picture= *(AVFrame*)&a->picture;
  102. *data_size = sizeof(AVPicture);
  103. emms_c();
  104. return buf_size;
  105. }
  106. #if CONFIG_VCR1_ENCODER
  107. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  108. VCR1Context * const a = avctx->priv_data;
  109. AVFrame *pict = data;
  110. AVFrame * const p= (AVFrame*)&a->picture;
  111. int size;
  112. *p = *pict;
  113. p->pict_type= AV_PICTURE_TYPE_I;
  114. p->key_frame= 1;
  115. emms_c();
  116. avpriv_align_put_bits(&a->pb);
  117. while(get_bit_count(&a->pb)&31)
  118. put_bits(&a->pb, 8, 0);
  119. size= get_bit_count(&a->pb)/32;
  120. return size*4;
  121. }
  122. #endif
  123. static av_cold void common_init(AVCodecContext *avctx){
  124. VCR1Context * const a = avctx->priv_data;
  125. avctx->coded_frame= (AVFrame*)&a->picture;
  126. avcodec_get_frame_defaults(&a->picture);
  127. a->avctx= avctx;
  128. }
  129. static av_cold int decode_init(AVCodecContext *avctx){
  130. common_init(avctx);
  131. avctx->pix_fmt= PIX_FMT_YUV410P;
  132. return 0;
  133. }
  134. static av_cold int decode_end(AVCodecContext *avctx){
  135. VCR1Context *s = avctx->priv_data;
  136. if (s->picture.data[0])
  137. avctx->release_buffer(avctx, &s->picture);
  138. return 0;
  139. }
  140. #if CONFIG_VCR1_ENCODER
  141. static av_cold int encode_init(AVCodecContext *avctx){
  142. common_init(avctx);
  143. return 0;
  144. }
  145. #endif
  146. AVCodec ff_vcr1_decoder = {
  147. .name = "vcr1",
  148. .type = AVMEDIA_TYPE_VIDEO,
  149. .id = CODEC_ID_VCR1,
  150. .priv_data_size = sizeof(VCR1Context),
  151. .init = decode_init,
  152. .close = decode_end,
  153. .decode = decode_frame,
  154. .capabilities = CODEC_CAP_DR1,
  155. .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
  156. };
  157. #if CONFIG_VCR1_ENCODER
  158. AVCodec ff_vcr1_encoder = {
  159. .name = "vcr1",
  160. .type = AVMEDIA_TYPE_VIDEO,
  161. .id = CODEC_ID_VCR1,
  162. .priv_data_size = sizeof(VCR1Context),
  163. .init = encode_init,
  164. .encode = encode_frame,
  165. .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
  166. };
  167. #endif