vcr1.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 libavcodec/vcr1.c
  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. const uint8_t *buf, int buf_size)
  41. {
  42. VCR1Context * const a = avctx->priv_data;
  43. AVFrame *picture = data;
  44. AVFrame * const p= (AVFrame*)&a->picture;
  45. const uint8_t *bytestream= buf;
  46. int i, x, y;
  47. if(p->data[0])
  48. avctx->release_buffer(avctx, p);
  49. p->reference= 0;
  50. if(avctx->get_buffer(avctx, p) < 0){
  51. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  52. return -1;
  53. }
  54. p->pict_type= FF_I_TYPE;
  55. p->key_frame= 1;
  56. for(i=0; i<16; i++){
  57. a->delta[i]= *(bytestream++);
  58. bytestream++;
  59. }
  60. for(y=0; y<avctx->height; y++){
  61. int offset;
  62. uint8_t *luma= &a->picture.data[0][ y*a->picture.linesize[0] ];
  63. if((y&3) == 0){
  64. uint8_t *cb= &a->picture.data[1][ (y>>2)*a->picture.linesize[1] ];
  65. uint8_t *cr= &a->picture.data[2][ (y>>2)*a->picture.linesize[2] ];
  66. for(i=0; i<4; i++)
  67. a->offset[i]= *(bytestream++);
  68. offset= a->offset[0] - a->delta[ bytestream[2]&0xF ];
  69. for(x=0; x<avctx->width; x+=4){
  70. luma[0]=( offset += a->delta[ bytestream[2]&0xF ]);
  71. luma[1]=( offset += a->delta[ bytestream[2]>>4 ]);
  72. luma[2]=( offset += a->delta[ bytestream[0]&0xF ]);
  73. luma[3]=( offset += a->delta[ bytestream[0]>>4 ]);
  74. luma += 4;
  75. *(cb++) = bytestream[3];
  76. *(cr++) = bytestream[1];
  77. bytestream+= 4;
  78. }
  79. }else{
  80. offset= a->offset[y&3] - a->delta[ bytestream[2]&0xF ];
  81. for(x=0; x<avctx->width; x+=8){
  82. luma[0]=( offset += a->delta[ bytestream[2]&0xF ]);
  83. luma[1]=( offset += a->delta[ bytestream[2]>>4 ]);
  84. luma[2]=( offset += a->delta[ bytestream[3]&0xF ]);
  85. luma[3]=( offset += a->delta[ bytestream[3]>>4 ]);
  86. luma[4]=( offset += a->delta[ bytestream[0]&0xF ]);
  87. luma[5]=( offset += a->delta[ bytestream[0]>>4 ]);
  88. luma[6]=( offset += a->delta[ bytestream[1]&0xF ]);
  89. luma[7]=( offset += a->delta[ bytestream[1]>>4 ]);
  90. luma += 8;
  91. bytestream+= 4;
  92. }
  93. }
  94. }
  95. *picture= *(AVFrame*)&a->picture;
  96. *data_size = sizeof(AVPicture);
  97. emms_c();
  98. return buf_size;
  99. }
  100. #if CONFIG_VCR1_ENCODER
  101. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  102. VCR1Context * const a = avctx->priv_data;
  103. AVFrame *pict = data;
  104. AVFrame * const p= (AVFrame*)&a->picture;
  105. int size;
  106. *p = *pict;
  107. p->pict_type= FF_I_TYPE;
  108. p->key_frame= 1;
  109. emms_c();
  110. align_put_bits(&a->pb);
  111. while(get_bit_count(&a->pb)&31)
  112. put_bits(&a->pb, 8, 0);
  113. size= get_bit_count(&a->pb)/32;
  114. return size*4;
  115. }
  116. #endif
  117. static av_cold void common_init(AVCodecContext *avctx){
  118. VCR1Context * const a = avctx->priv_data;
  119. avctx->coded_frame= (AVFrame*)&a->picture;
  120. a->avctx= avctx;
  121. }
  122. static av_cold int decode_init(AVCodecContext *avctx){
  123. common_init(avctx);
  124. avctx->pix_fmt= PIX_FMT_YUV410P;
  125. return 0;
  126. }
  127. #if CONFIG_VCR1_ENCODER
  128. static av_cold int encode_init(AVCodecContext *avctx){
  129. common_init(avctx);
  130. return 0;
  131. }
  132. #endif
  133. AVCodec vcr1_decoder = {
  134. "vcr1",
  135. CODEC_TYPE_VIDEO,
  136. CODEC_ID_VCR1,
  137. sizeof(VCR1Context),
  138. decode_init,
  139. NULL,
  140. NULL,
  141. decode_frame,
  142. CODEC_CAP_DR1,
  143. .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
  144. };
  145. #if CONFIG_VCR1_ENCODER
  146. AVCodec vcr1_encoder = {
  147. "vcr1",
  148. CODEC_TYPE_VIDEO,
  149. CODEC_ID_VCR1,
  150. sizeof(VCR1Context),
  151. encode_init,
  152. encode_frame,
  153. //encode_end,
  154. .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
  155. };
  156. #endif