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.

154 lines
4.4KB

  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 ((ret = ff_get_buffer(avctx, pic, AV_GET_BUFFER_FLAG_REF)) < 0)
  54. return ret;
  55. zstream->next_in = avpkt->data;
  56. zstream->avail_in = avpkt->size;
  57. dst = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
  58. /**
  59. * ZeroCodec has very simple interframe compression. If a value
  60. * is the same as the previous frame, set it to 0.
  61. */
  62. for (i = 0; i < avctx->height; i++) {
  63. zstream->next_out = dst;
  64. zstream->avail_out = avctx->width << 1;
  65. zret = inflate(zstream, Z_SYNC_FLUSH);
  66. if (zret != Z_OK && zret != Z_STREAM_END) {
  67. av_log(avctx, AV_LOG_ERROR,
  68. "Inflate failed with return code: %d.\n", zret);
  69. return AVERROR_INVALIDDATA;
  70. }
  71. if (!(avpkt->flags & AV_PKT_FLAG_KEY))
  72. for (j = 0; j < avctx->width << 1; j++)
  73. dst[j] += prev[j] & -!dst[j];
  74. prev -= prev_pic->linesize[0];
  75. dst -= pic->linesize[0];
  76. }
  77. av_frame_unref(zc->previous_frame);
  78. if ((ret = av_frame_ref(zc->previous_frame, pic)) < 0)
  79. return ret;
  80. *got_frame = 1;
  81. return avpkt->size;
  82. }
  83. static av_cold int zerocodec_decode_close(AVCodecContext *avctx)
  84. {
  85. ZeroCodecContext *zc = avctx->priv_data;
  86. av_frame_free(&zc->previous_frame);
  87. inflateEnd(&zc->zstream);
  88. return 0;
  89. }
  90. static av_cold int zerocodec_decode_init(AVCodecContext *avctx)
  91. {
  92. ZeroCodecContext *zc = avctx->priv_data;
  93. z_stream *zstream = &zc->zstream;
  94. int zret;
  95. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  96. avctx->bits_per_raw_sample = 8;
  97. zstream->zalloc = Z_NULL;
  98. zstream->zfree = Z_NULL;
  99. zstream->opaque = Z_NULL;
  100. zret = inflateInit(zstream);
  101. if (zret != Z_OK) {
  102. av_log(avctx, AV_LOG_ERROR, "Could not initialize inflate: %d.\n", zret);
  103. return AVERROR(ENOMEM);
  104. }
  105. zc->previous_frame = av_frame_alloc();
  106. if (!zc->previous_frame) {
  107. zerocodec_decode_close(avctx);
  108. return AVERROR(ENOMEM);
  109. }
  110. return 0;
  111. }
  112. AVCodec ff_zerocodec_decoder = {
  113. .type = AVMEDIA_TYPE_VIDEO,
  114. .name = "zerocodec",
  115. .long_name = NULL_IF_CONFIG_SMALL("ZeroCodec Lossless Video"),
  116. .id = AV_CODEC_ID_ZEROCODEC,
  117. .priv_data_size = sizeof(ZeroCodecContext),
  118. .init = zerocodec_decode_init,
  119. .decode = zerocodec_decode_frame,
  120. .close = zerocodec_decode_close,
  121. .capabilities = AV_CODEC_CAP_DR1,
  122. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  123. };