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.

200 lines
5.8KB

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