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.

151 lines
4.7KB

  1. /*
  2. * Forward Uncompressed
  3. *
  4. * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #include "libavutil/opt.h"
  26. typedef struct {
  27. AVClass *av_class;
  28. int change_field_order;
  29. } FRWUContext;
  30. static av_cold int decode_init(AVCodecContext *avctx)
  31. {
  32. if (avctx->width & 1) {
  33. av_log(avctx, AV_LOG_ERROR, "frwu needs even width\n");
  34. return AVERROR(EINVAL);
  35. }
  36. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  37. avctx->coded_frame = avcodec_alloc_frame();
  38. if (!avctx->coded_frame)
  39. return AVERROR(ENOMEM);
  40. return 0;
  41. }
  42. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  43. AVPacket *avpkt)
  44. {
  45. FRWUContext *s = avctx->priv_data;
  46. int field, ret;
  47. AVFrame *pic = avctx->coded_frame;
  48. const uint8_t *buf = avpkt->data;
  49. const uint8_t *buf_end = buf + avpkt->size;
  50. if (pic->data[0])
  51. avctx->release_buffer(avctx, pic);
  52. if (avpkt->size < avctx->width * 2 * avctx->height + 4 + 2*8) {
  53. av_log(avctx, AV_LOG_ERROR, "Packet is too small.\n");
  54. return AVERROR_INVALIDDATA;
  55. }
  56. if (bytestream_get_le32(&buf) != MKTAG('F', 'R', 'W', '1')) {
  57. av_log(avctx, AV_LOG_ERROR, "incorrect marker\n");
  58. return AVERROR_INVALIDDATA;
  59. }
  60. pic->reference = 0;
  61. if ((ret = ff_get_buffer(avctx, pic)) < 0) {
  62. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  63. return ret;
  64. }
  65. pic->pict_type = AV_PICTURE_TYPE_I;
  66. pic->key_frame = 1;
  67. for (field = 0; field < 2; field++) {
  68. int i;
  69. int field_h = (avctx->height + !field) >> 1;
  70. int field_size, min_field_size = avctx->width * 2 * field_h;
  71. uint8_t *dst = pic->data[0];
  72. if (buf_end - buf < 8)
  73. return AVERROR_INVALIDDATA;
  74. buf += 4; // flags? 0x80 == bottom field maybe?
  75. field_size = bytestream_get_le32(&buf);
  76. if (field_size < min_field_size) {
  77. av_log(avctx, AV_LOG_ERROR, "Field size %i is too small (required %i)\n", field_size, min_field_size);
  78. return AVERROR_INVALIDDATA;
  79. }
  80. if (buf_end - buf < field_size) {
  81. av_log(avctx, AV_LOG_ERROR, "Packet is too small, need %i, have %i\n", field_size, (int)(buf_end - buf));
  82. return AVERROR_INVALIDDATA;
  83. }
  84. if (field ^ s->change_field_order) {
  85. dst += pic->linesize[0];
  86. } else if (s->change_field_order) {
  87. dst += 2 * pic->linesize[0];
  88. }
  89. for (i = 0; i < field_h; i++) {
  90. if (s->change_field_order && field && i == field_h - 1)
  91. dst = pic->data[0];
  92. memcpy(dst, buf, avctx->width * 2);
  93. buf += avctx->width * 2;
  94. dst += pic->linesize[0] << 1;
  95. }
  96. buf += field_size - min_field_size;
  97. }
  98. *got_frame = 1;
  99. *(AVFrame*)data = *pic;
  100. return avpkt->size;
  101. }
  102. static av_cold int decode_close(AVCodecContext *avctx)
  103. {
  104. AVFrame *pic = avctx->coded_frame;
  105. if (pic->data[0])
  106. avctx->release_buffer(avctx, pic);
  107. av_freep(&avctx->coded_frame);
  108. return 0;
  109. }
  110. static const AVOption frwu_options[] = {
  111. {"change_field_order", "Change field order", offsetof(FRWUContext, change_field_order), FF_OPT_TYPE_INT,
  112. {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM},
  113. {NULL}
  114. };
  115. static const AVClass frwu_class = {
  116. .class_name = "frwu Decoder",
  117. .item_name = av_default_item_name,
  118. .option = frwu_options,
  119. .version = LIBAVUTIL_VERSION_INT,
  120. };
  121. AVCodec ff_frwu_decoder = {
  122. .name = "frwu",
  123. .type = AVMEDIA_TYPE_VIDEO,
  124. .id = AV_CODEC_ID_FRWU,
  125. .priv_data_size = sizeof(FRWUContext),
  126. .init = decode_init,
  127. .close = decode_close,
  128. .decode = decode_frame,
  129. .capabilities = CODEC_CAP_DR1,
  130. .long_name = NULL_IF_CONFIG_SMALL("Forward Uncompressed"),
  131. .priv_class = &frwu_class,
  132. };