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.

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