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.

183 lines
5.3KB

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