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.

167 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. static av_cold int encode_init(AVCodecContext *avctx)
  89. {
  90. avctx->coded_frame = av_frame_alloc();
  91. if (!avctx->coded_frame)
  92. return AVERROR(ENOMEM);
  93. return 0;
  94. }
  95. static av_cold int encode_close(AVCodecContext *avctx)
  96. {
  97. av_frame_free(&avctx->coded_frame);
  98. return 0;
  99. }
  100. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  101. const AVFrame *p, int *got_packet)
  102. {
  103. PutBitContext pb;
  104. int x, y, ret;
  105. if ((ret = ff_alloc_packet(pkt, 32*avctx->height*avctx->width/4)) < 0) {
  106. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  107. return ret;
  108. }
  109. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  110. avctx->coded_frame->key_frame = 1;
  111. init_put_bits(&pb, pkt->data, pkt->size);
  112. for (y = 0; y < avctx->height; y++) {
  113. uint8_t *luma = &p->data[0][y * p->linesize[0]];
  114. uint8_t *cb = &p->data[1][y * p->linesize[1]];
  115. uint8_t *cr = &p->data[2][y * p->linesize[2]];
  116. for (x = 0; x < avctx->width; x += 4) {
  117. put_bits(&pb, 5, luma[3] >> 3);
  118. put_bits(&pb, 5, luma[2] >> 3);
  119. put_bits(&pb, 5, luma[1] >> 3);
  120. put_bits(&pb, 5, luma[0] >> 3);
  121. luma += 4;
  122. put_bits(&pb, 6, *(cb++) >> 2);
  123. put_bits(&pb, 6, *(cr++) >> 2);
  124. }
  125. }
  126. flush_put_bits(&pb);
  127. pkt->size = put_bits_count(&pb) / 8;
  128. pkt->flags |= AV_PKT_FLAG_KEY;
  129. *got_packet = 1;
  130. return 0;
  131. }
  132. AVCodec ff_cljr_encoder = {
  133. .name = "cljr",
  134. .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
  135. .type = AVMEDIA_TYPE_VIDEO,
  136. .id = AV_CODEC_ID_CLJR,
  137. .init = encode_init,
  138. .encode2 = encode_frame,
  139. .close = encode_close,
  140. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
  141. AV_PIX_FMT_NONE },
  142. };
  143. #endif