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.

175 lines
5.0KB

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