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.

201 lines
5.0KB

  1. /*
  2. * ATI VCR1 codec
  3. * Copyright (c) 2003 Michael Niedermayer
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file vcr1.c
  21. * ati vcr1 codec.
  22. */
  23. #include "avcodec.h"
  24. #include "mpegvideo.h"
  25. //#undef NDEBUG
  26. //#include <assert.h>
  27. typedef struct VCR1Context{
  28. AVCodecContext *avctx;
  29. AVFrame picture;
  30. int delta[16];
  31. int offset[4];
  32. } VCR1Context;
  33. static int decode_frame(AVCodecContext *avctx,
  34. void *data, int *data_size,
  35. uint8_t *buf, int buf_size)
  36. {
  37. VCR1Context * const a = avctx->priv_data;
  38. AVFrame *picture = data;
  39. AVFrame * const p= (AVFrame*)&a->picture;
  40. uint8_t *bytestream= buf;
  41. int i, x, y;
  42. *data_size = 0;
  43. /* special case for last picture */
  44. if (buf_size == 0) {
  45. return 0;
  46. }
  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= 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 0
  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. int mb_x, mb_y;
  107. *p = *pict;
  108. p->pict_type= I_TYPE;
  109. p->key_frame= 1;
  110. emms_c();
  111. align_put_bits(&a->pb);
  112. while(get_bit_count(&a->pb)&31)
  113. put_bits(&a->pb, 8, 0);
  114. size= get_bit_count(&a->pb)/32;
  115. return size*4;
  116. }
  117. #endif
  118. static void common_init(AVCodecContext *avctx){
  119. VCR1Context * const a = avctx->priv_data;
  120. avctx->coded_frame= (AVFrame*)&a->picture;
  121. a->avctx= avctx;
  122. }
  123. static int decode_init(AVCodecContext *avctx){
  124. common_init(avctx);
  125. avctx->pix_fmt= PIX_FMT_YUV410P;
  126. return 0;
  127. }
  128. static int encode_init(AVCodecContext *avctx){
  129. common_init(avctx);
  130. return 0;
  131. }
  132. static int decode_end(AVCodecContext *avctx){
  133. avcodec_default_free_buffers(avctx);
  134. return 0;
  135. }
  136. AVCodec vcr1_decoder = {
  137. "vcr1",
  138. CODEC_TYPE_VIDEO,
  139. CODEC_ID_VCR1,
  140. sizeof(VCR1Context),
  141. decode_init,
  142. NULL,
  143. decode_end,
  144. decode_frame,
  145. CODEC_CAP_DR1,
  146. };
  147. #if 0
  148. #ifdef CONFIG_ENCODERS
  149. AVCodec vcr1_encoder = {
  150. "vcr1",
  151. CODEC_TYPE_VIDEO,
  152. CODEC_ID_VCR1,
  153. sizeof(VCR1Context),
  154. encode_init,
  155. encode_frame,
  156. //encode_end,
  157. };
  158. #endif //CONFIG_ENCODERS
  159. #endif