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.

156 lines
4.5KB

  1. /*
  2. * ZeroCodec Decoder
  3. *
  4. * Copyright (c) 2012, Derek Buitenhuis
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <zlib.h>
  19. #include "avcodec.h"
  20. #include "internal.h"
  21. #include "libavutil/common.h"
  22. typedef struct ZeroCodecContext {
  23. AVFrame *previous_frame;
  24. z_stream zstream;
  25. } ZeroCodecContext;
  26. static int zerocodec_decode_frame(AVCodecContext *avctx, void *data,
  27. int *got_frame, AVPacket *avpkt)
  28. {
  29. ZeroCodecContext *zc = avctx->priv_data;
  30. AVFrame *pic = data;
  31. AVFrame *prev_pic = zc->previous_frame;
  32. z_stream *zstream = &zc->zstream;
  33. uint8_t *prev = prev_pic->data[0];
  34. uint8_t *dst;
  35. int i, j, zret, ret;
  36. if (avpkt->flags & AV_PKT_FLAG_KEY) {
  37. pic->key_frame = 1;
  38. pic->pict_type = AV_PICTURE_TYPE_I;
  39. } else {
  40. if (!prev) {
  41. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  42. return AVERROR_INVALIDDATA;
  43. }
  44. prev += (avctx->height - 1) * prev_pic->linesize[0];
  45. pic->key_frame = 0;
  46. pic->pict_type = AV_PICTURE_TYPE_P;
  47. }
  48. zret = inflateReset(zstream);
  49. if (zret != Z_OK) {
  50. av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret);
  51. return AVERROR_INVALIDDATA;
  52. }
  53. if (ff_get_buffer(avctx, pic, AV_GET_BUFFER_FLAG_REF) < 0) {
  54. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  55. return AVERROR(ENOMEM);
  56. }
  57. zstream->next_in = avpkt->data;
  58. zstream->avail_in = avpkt->size;
  59. dst = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
  60. /**
  61. * ZeroCodec has very simple interframe compression. If a value
  62. * is the same as the previous frame, set it to 0.
  63. */
  64. for (i = 0; i < avctx->height; i++) {
  65. zstream->next_out = dst;
  66. zstream->avail_out = avctx->width << 1;
  67. zret = inflate(zstream, Z_SYNC_FLUSH);
  68. if (zret != Z_OK && zret != Z_STREAM_END) {
  69. av_log(avctx, AV_LOG_ERROR,
  70. "Inflate failed with return code: %d.\n", zret);
  71. return AVERROR_INVALIDDATA;
  72. }
  73. if (!(avpkt->flags & AV_PKT_FLAG_KEY))
  74. for (j = 0; j < avctx->width << 1; j++)
  75. dst[j] += prev[j] & -!dst[j];
  76. prev -= prev_pic->linesize[0];
  77. dst -= pic->linesize[0];
  78. }
  79. av_frame_unref(zc->previous_frame);
  80. if ((ret = av_frame_ref(zc->previous_frame, pic)) < 0)
  81. return ret;
  82. *got_frame = 1;
  83. return avpkt->size;
  84. }
  85. static av_cold int zerocodec_decode_close(AVCodecContext *avctx)
  86. {
  87. ZeroCodecContext *zc = avctx->priv_data;
  88. av_frame_free(&zc->previous_frame);
  89. inflateEnd(&zc->zstream);
  90. return 0;
  91. }
  92. static av_cold int zerocodec_decode_init(AVCodecContext *avctx)
  93. {
  94. ZeroCodecContext *zc = avctx->priv_data;
  95. z_stream *zstream = &zc->zstream;
  96. int zret;
  97. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  98. avctx->bits_per_raw_sample = 8;
  99. zstream->zalloc = Z_NULL;
  100. zstream->zfree = Z_NULL;
  101. zstream->opaque = Z_NULL;
  102. zret = inflateInit(zstream);
  103. if (zret != Z_OK) {
  104. av_log(avctx, AV_LOG_ERROR, "Could not initialize inflate: %d.\n", zret);
  105. return AVERROR(ENOMEM);
  106. }
  107. zc->previous_frame = av_frame_alloc();
  108. if (!zc->previous_frame) {
  109. zerocodec_decode_close(avctx);
  110. return AVERROR(ENOMEM);
  111. }
  112. return 0;
  113. }
  114. AVCodec ff_zerocodec_decoder = {
  115. .type = AVMEDIA_TYPE_VIDEO,
  116. .name = "zerocodec",
  117. .long_name = NULL_IF_CONFIG_SMALL("ZeroCodec Lossless Video"),
  118. .id = AV_CODEC_ID_ZEROCODEC,
  119. .priv_data_size = sizeof(ZeroCodecContext),
  120. .init = zerocodec_decode_init,
  121. .decode = zerocodec_decode_frame,
  122. .close = zerocodec_decode_close,
  123. .capabilities = AV_CODEC_CAP_DR1,
  124. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  125. };