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.

189 lines
5.4KB

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