qoidec.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * QOI image format
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdlib.h>
  21. #include "libavutil/imgutils.h"
  22. #include "avcodec.h"
  23. #include "internal.h"
  24. #include "bytestream.h"
  25. #include "codec_internal.h"
  26. #include "thread.h"
  27. #include "qoi.h"
  28. static int qoi_decode_frame(AVCodecContext *avctx, AVFrame *p,
  29. int *got_frame, AVPacket *avpkt)
  30. {
  31. const uint8_t *buf = avpkt->data;
  32. int ret, buf_size = avpkt->size;
  33. int width, height, channels, space, run = 0;
  34. uint8_t index[64][4] = { 0 };
  35. uint8_t px[4] = { 0, 0, 0, 255 };
  36. GetByteContext gb;
  37. uint8_t *dst;
  38. uint64_t len;
  39. if (buf_size < 20)
  40. return AVERROR_INVALIDDATA;
  41. bytestream2_init(&gb, buf, buf_size);
  42. bytestream2_skip(&gb, 4);
  43. width = bytestream2_get_be32(&gb);
  44. height = bytestream2_get_be32(&gb);
  45. channels = bytestream2_get_byte(&gb);
  46. space = bytestream2_get_byte(&gb);
  47. switch (space) {
  48. case 0: break;
  49. case 1: avctx->color_trc = AVCOL_TRC_LINEAR; break;
  50. default: return AVERROR_INVALIDDATA;
  51. }
  52. if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
  53. return ret;
  54. switch (channels) {
  55. case 3: avctx->pix_fmt = AV_PIX_FMT_RGB24; break;
  56. case 4: avctx->pix_fmt = AV_PIX_FMT_RGBA; break;
  57. default: return AVERROR_INVALIDDATA;
  58. }
  59. if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0)
  60. return ret;
  61. dst = p->data[0];
  62. len = width * height * channels;
  63. for (int n = 0, off_x = 0; n < len; n += channels, off_x++) {
  64. if (off_x >= width) {
  65. off_x = 0;
  66. dst += p->linesize[0];
  67. }
  68. if (run > 0) {
  69. run--;
  70. } else if (bytestream2_get_bytes_left(&gb) > 4) {
  71. int chunk = bytestream2_get_byteu(&gb);
  72. if (chunk == QOI_OP_RGB) {
  73. bytestream2_get_bufferu(&gb, px, 3);
  74. } else if (chunk == QOI_OP_RGBA) {
  75. bytestream2_get_bufferu(&gb, px, 4);
  76. } else if ((chunk & QOI_MASK_2) == QOI_OP_INDEX) {
  77. memcpy(px, index[chunk], 4);
  78. } else if ((chunk & QOI_MASK_2) == QOI_OP_DIFF) {
  79. px[0] += ((chunk >> 4) & 0x03) - 2;
  80. px[1] += ((chunk >> 2) & 0x03) - 2;
  81. px[2] += ( chunk & 0x03) - 2;
  82. } else if ((chunk & QOI_MASK_2) == QOI_OP_LUMA) {
  83. int b2 = bytestream2_get_byteu(&gb);
  84. int vg = (chunk & 0x3f) - 32;
  85. px[0] += vg - 8 + ((b2 >> 4) & 0x0f);
  86. px[1] += vg;
  87. px[2] += vg - 8 + (b2 & 0x0f);
  88. } else if ((chunk & QOI_MASK_2) == QOI_OP_RUN) {
  89. run = chunk & 0x3f;
  90. }
  91. memcpy(index[QOI_COLOR_HASH(px) & 63], px, 4);
  92. } else {
  93. break;
  94. }
  95. memcpy(&dst[off_x * channels], px, channels);
  96. }
  97. p->key_frame = 1;
  98. p->pict_type = AV_PICTURE_TYPE_I;
  99. *got_frame = 1;
  100. return buf_size;
  101. }
  102. const FFCodec ff_qoi_decoder = {
  103. .p.name = "qoi",
  104. .p.long_name = NULL_IF_CONFIG_SMALL("QOI (Quite OK Image format) image"),
  105. .p.type = AVMEDIA_TYPE_VIDEO,
  106. .p.id = AV_CODEC_ID_QOI,
  107. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  108. FF_CODEC_DECODE_CB(qoi_decode_frame),
  109. };