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.

172 lines
4.9KB

  1. /*
  2. * Cirrus Logic AccuPak (CLJR) codec
  3. * Copyright (c) 2003 Alex Beregszaszi
  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. * Cirrus Logic AccuPak codec.
  24. */
  25. #include "avcodec.h"
  26. #include "get_bits.h"
  27. #include "put_bits.h"
  28. typedef struct CLJRContext {
  29. AVCodecContext *avctx;
  30. AVFrame picture;
  31. } CLJRContext;
  32. static av_cold int common_init(AVCodecContext *avctx)
  33. {
  34. CLJRContext * const a = avctx->priv_data;
  35. avctx->coded_frame = &a->picture;
  36. a->avctx = avctx;
  37. return 0;
  38. }
  39. #if CONFIG_CLJR_DECODER
  40. static int decode_frame(AVCodecContext *avctx,
  41. void *data, int *data_size,
  42. AVPacket *avpkt)
  43. {
  44. const uint8_t *buf = avpkt->data;
  45. int buf_size = avpkt->size;
  46. CLJRContext * const a = avctx->priv_data;
  47. GetBitContext gb;
  48. AVFrame *picture = data;
  49. AVFrame * const p = &a->picture;
  50. int x, y;
  51. if (p->data[0])
  52. avctx->release_buffer(avctx, p);
  53. if (buf_size / avctx->height < avctx->width) {
  54. av_log(avctx, AV_LOG_ERROR,
  55. "Resolution larger than buffer size. Invalid header?\n");
  56. return AVERROR_INVALIDDATA;
  57. }
  58. p->reference = 0;
  59. if (avctx->get_buffer(avctx, p) < 0) {
  60. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  61. return -1;
  62. }
  63. p->pict_type = AV_PICTURE_TYPE_I;
  64. p->key_frame = 1;
  65. init_get_bits(&gb, buf, buf_size * 8);
  66. for (y = 0; y < avctx->height; y++) {
  67. uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
  68. uint8_t *cb = &a->picture.data[1][y * a->picture.linesize[1]];
  69. uint8_t *cr = &a->picture.data[2][y * a->picture.linesize[2]];
  70. for (x = 0; x < avctx->width; x += 4) {
  71. luma[3] = get_bits(&gb, 5) << 3;
  72. luma[2] = get_bits(&gb, 5) << 3;
  73. luma[1] = get_bits(&gb, 5) << 3;
  74. luma[0] = get_bits(&gb, 5) << 3;
  75. luma += 4;
  76. *(cb++) = get_bits(&gb, 6) << 2;
  77. *(cr++) = get_bits(&gb, 6) << 2;
  78. }
  79. }
  80. *picture = a->picture;
  81. *data_size = sizeof(AVPicture);
  82. return buf_size;
  83. }
  84. static av_cold int decode_init(AVCodecContext *avctx)
  85. {
  86. avctx->pix_fmt = PIX_FMT_YUV411P;
  87. return common_init(avctx);
  88. }
  89. static av_cold int decode_end(AVCodecContext *avctx)
  90. {
  91. CLJRContext *a = avctx->priv_data;
  92. if (a->picture.data[0])
  93. avctx->release_buffer(avctx, &a->picture);
  94. return 0;
  95. }
  96. AVCodec ff_cljr_decoder = {
  97. .name = "cljr",
  98. .type = AVMEDIA_TYPE_VIDEO,
  99. .id = CODEC_ID_CLJR,
  100. .priv_data_size = sizeof(CLJRContext),
  101. .init = decode_init,
  102. .close = decode_end,
  103. .decode = decode_frame,
  104. .capabilities = CODEC_CAP_DR1,
  105. .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
  106. };
  107. #endif
  108. #if CONFIG_CLJR_ENCODER
  109. static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
  110. int buf_size, void *data)
  111. {
  112. PutBitContext pb;
  113. AVFrame *p = data;
  114. int x, y;
  115. p->pict_type = AV_PICTURE_TYPE_I;
  116. p->key_frame = 1;
  117. init_put_bits(&pb, buf, buf_size / 8);
  118. for (y = 0; y < avctx->height; y++) {
  119. uint8_t *luma = &p->data[0][y * p->linesize[0]];
  120. uint8_t *cb = &p->data[1][y * p->linesize[1]];
  121. uint8_t *cr = &p->data[2][y * p->linesize[2]];
  122. for (x = 0; x < avctx->width; x += 4) {
  123. put_bits(&pb, 5, luma[3] >> 3);
  124. put_bits(&pb, 5, luma[2] >> 3);
  125. put_bits(&pb, 5, luma[1] >> 3);
  126. put_bits(&pb, 5, luma[0] >> 3);
  127. luma += 4;
  128. put_bits(&pb, 6, *(cb++) >> 2);
  129. put_bits(&pb, 6, *(cr++) >> 2);
  130. }
  131. }
  132. flush_put_bits(&pb);
  133. return put_bits_count(&pb) / 8;
  134. }
  135. AVCodec ff_cljr_encoder = {
  136. .name = "cljr",
  137. .type = AVMEDIA_TYPE_VIDEO,
  138. .id = CODEC_ID_CLJR,
  139. .priv_data_size = sizeof(CLJRContext),
  140. .init = common_init,
  141. .encode = encode_frame,
  142. .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_YUV411P,
  143. PIX_FMT_NONE },
  144. .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
  145. };
  146. #endif