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.

126 lines
3.9KB

  1. /*
  2. * Forward Uncompressed
  3. *
  4. * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "internal.h"
  25. static av_cold int decode_init(AVCodecContext *avctx)
  26. {
  27. if (avctx->width & 1) {
  28. av_log(avctx, AV_LOG_ERROR, "frwu needs even width\n");
  29. return AVERROR(EINVAL);
  30. }
  31. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  32. avctx->coded_frame = avcodec_alloc_frame();
  33. if (!avctx->coded_frame)
  34. return AVERROR(ENOMEM);
  35. return 0;
  36. }
  37. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  38. AVPacket *avpkt)
  39. {
  40. int field, ret;
  41. AVFrame *pic = avctx->coded_frame;
  42. const uint8_t *buf = avpkt->data;
  43. const uint8_t *buf_end = buf + avpkt->size;
  44. if (pic->data[0])
  45. avctx->release_buffer(avctx, pic);
  46. if (avpkt->size < avctx->width * 2 * avctx->height + 4 + 2*8) {
  47. av_log(avctx, AV_LOG_ERROR, "Packet is too small.\n");
  48. return AVERROR_INVALIDDATA;
  49. }
  50. if (bytestream_get_le32(&buf) != MKTAG('F', 'R', 'W', '1')) {
  51. av_log(avctx, AV_LOG_ERROR, "incorrect marker\n");
  52. return AVERROR_INVALIDDATA;
  53. }
  54. pic->reference = 0;
  55. if ((ret = ff_get_buffer(avctx, pic)) < 0) {
  56. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  57. return ret;
  58. }
  59. pic->pict_type = AV_PICTURE_TYPE_I;
  60. pic->key_frame = 1;
  61. pic->interlaced_frame = 1;
  62. pic->top_field_first = 1;
  63. for (field = 0; field < 2; field++) {
  64. int i;
  65. int field_h = (avctx->height + !field) >> 1;
  66. int field_size, min_field_size = avctx->width * 2 * field_h;
  67. uint8_t *dst = pic->data[0];
  68. if (buf_end - buf < 8)
  69. return AVERROR_INVALIDDATA;
  70. buf += 4; // flags? 0x80 == bottom field maybe?
  71. field_size = bytestream_get_le32(&buf);
  72. if (field_size < min_field_size) {
  73. av_log(avctx, AV_LOG_ERROR, "Field size %i is too small (required %i)\n", field_size, min_field_size);
  74. return AVERROR_INVALIDDATA;
  75. }
  76. if (buf_end - buf < field_size) {
  77. av_log(avctx, AV_LOG_ERROR, "Packet is too small, need %i, have %i\n", field_size, (int)(buf_end - buf));
  78. return AVERROR_INVALIDDATA;
  79. }
  80. if (field)
  81. dst += pic->linesize[0];
  82. for (i = 0; i < field_h; i++) {
  83. memcpy(dst, buf, avctx->width * 2);
  84. buf += avctx->width * 2;
  85. dst += pic->linesize[0] << 1;
  86. }
  87. buf += field_size - min_field_size;
  88. }
  89. *got_frame = 1;
  90. *(AVFrame*)data = *pic;
  91. return avpkt->size;
  92. }
  93. static av_cold int decode_close(AVCodecContext *avctx)
  94. {
  95. AVFrame *pic = avctx->coded_frame;
  96. if (pic->data[0])
  97. avctx->release_buffer(avctx, pic);
  98. av_freep(&avctx->coded_frame);
  99. return 0;
  100. }
  101. AVCodec ff_frwu_decoder = {
  102. .name = "frwu",
  103. .type = AVMEDIA_TYPE_VIDEO,
  104. .id = AV_CODEC_ID_FRWU,
  105. .init = decode_init,
  106. .close = decode_close,
  107. .decode = decode_frame,
  108. .capabilities = CODEC_CAP_DR1,
  109. .long_name = NULL_IF_CONFIG_SMALL("Forward Uncompressed"),
  110. };