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.

165 lines
4.8KB

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