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.

191 lines
5.6KB

  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. typedef struct VCR1Context {
  28. AVFrame picture;
  29. int delta[16];
  30. int offset[4];
  31. } VCR1Context;
  32. static av_cold void common_init(AVCodecContext *avctx)
  33. {
  34. VCR1Context *const a = avctx->priv_data;
  35. avctx->coded_frame = &a->picture;
  36. avcodec_get_frame_defaults(&a->picture);
  37. }
  38. static av_cold int decode_init(AVCodecContext *avctx)
  39. {
  40. common_init(avctx);
  41. avctx->pix_fmt = PIX_FMT_YUV410P;
  42. return 0;
  43. }
  44. static av_cold int decode_end(AVCodecContext *avctx)
  45. {
  46. VCR1Context *s = avctx->priv_data;
  47. if (s->picture.data[0])
  48. avctx->release_buffer(avctx, &s->picture);
  49. return 0;
  50. }
  51. static int decode_frame(AVCodecContext *avctx, void *data,
  52. int *data_size, AVPacket *avpkt)
  53. {
  54. const uint8_t *buf = avpkt->data;
  55. int buf_size = avpkt->size;
  56. VCR1Context *const a = avctx->priv_data;
  57. AVFrame *picture = data;
  58. AVFrame *const p = &a->picture;
  59. const uint8_t *bytestream = buf;
  60. int i, x, y;
  61. if (p->data[0])
  62. avctx->release_buffer(avctx, p);
  63. if(buf_size < 16 + avctx->height + avctx->width*avctx->height*5/8){
  64. av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
  65. return AVERROR(EINVAL);
  66. }
  67. p->reference = 0;
  68. if (avctx->get_buffer(avctx, p) < 0) {
  69. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  70. return -1;
  71. }
  72. p->pict_type = AV_PICTURE_TYPE_I;
  73. p->key_frame = 1;
  74. for (i = 0; i < 16; i++) {
  75. a->delta[i] = *bytestream++;
  76. bytestream++;
  77. }
  78. for (y = 0; y < avctx->height; y++) {
  79. int offset;
  80. uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
  81. if ((y & 3) == 0) {
  82. uint8_t *cb = &a->picture.data[1][(y >> 2) * a->picture.linesize[1]];
  83. uint8_t *cr = &a->picture.data[2][(y >> 2) * a->picture.linesize[2]];
  84. for (i = 0; i < 4; i++)
  85. a->offset[i] = *bytestream++;
  86. offset = a->offset[0] - a->delta[bytestream[2] & 0xF];
  87. for (x = 0; x < avctx->width; x += 4) {
  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[0] & 0xF];
  91. luma[3] = offset += a->delta[bytestream[0] >> 4];
  92. luma += 4;
  93. *cb++ = bytestream[3];
  94. *cr++ = bytestream[1];
  95. bytestream += 4;
  96. }
  97. } else {
  98. offset = a->offset[y & 3] - a->delta[bytestream[2] & 0xF];
  99. for (x = 0; x < avctx->width; x += 8) {
  100. luma[0] = offset += a->delta[bytestream[2] & 0xF];
  101. luma[1] = offset += a->delta[bytestream[2] >> 4];
  102. luma[2] = offset += a->delta[bytestream[3] & 0xF];
  103. luma[3] = offset += a->delta[bytestream[3] >> 4];
  104. luma[4] = offset += a->delta[bytestream[0] & 0xF];
  105. luma[5] = offset += a->delta[bytestream[0] >> 4];
  106. luma[6] = offset += a->delta[bytestream[1] & 0xF];
  107. luma[7] = offset += a->delta[bytestream[1] >> 4];
  108. luma += 8;
  109. bytestream += 4;
  110. }
  111. }
  112. }
  113. *picture = a->picture;
  114. *data_size = sizeof(AVPicture);
  115. return buf_size;
  116. }
  117. AVCodec ff_vcr1_decoder = {
  118. .name = "vcr1",
  119. .type = AVMEDIA_TYPE_VIDEO,
  120. .id = CODEC_ID_VCR1,
  121. .priv_data_size = sizeof(VCR1Context),
  122. .init = decode_init,
  123. .close = decode_end,
  124. .decode = decode_frame,
  125. .capabilities = CODEC_CAP_DR1,
  126. .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
  127. };
  128. /* Disable the encoder. */
  129. #undef CONFIG_VCR1_ENCODER
  130. #define CONFIG_VCR1_ENCODER 0
  131. #if CONFIG_VCR1_ENCODER
  132. static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
  133. int buf_size, void *data)
  134. {
  135. VCR1Context *const a = avctx->priv_data;
  136. AVFrame *pict = data;
  137. AVFrame *const p = &a->picture;
  138. int size;
  139. *p = *pict;
  140. p->pict_type = AV_PICTURE_TYPE_I;
  141. p->key_frame = 1;
  142. avpriv_align_put_bits(&a->pb);
  143. while (get_bit_count(&a->pb) & 31)
  144. put_bits(&a->pb, 8, 0);
  145. size = get_bit_count(&a->pb) / 32;
  146. return size * 4;
  147. }
  148. AVCodec ff_vcr1_encoder = {
  149. .name = "vcr1",
  150. .type = AVMEDIA_TYPE_VIDEO,
  151. .id = CODEC_ID_VCR1,
  152. .priv_data_size = sizeof(VCR1Context),
  153. .init = common_init,
  154. .encode = encode_frame,
  155. .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
  156. };
  157. #endif /* CONFIG_VCR1_ENCODER */