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.

129 lines
4.1KB

  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. return 0;
  38. }
  39. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  40. AVPacket *avpkt)
  41. {
  42. FRWUContext *s = avctx->priv_data;
  43. int field, ret;
  44. AVFrame *pic = data;
  45. const uint8_t *buf = avpkt->data;
  46. const uint8_t *buf_end = buf + avpkt->size;
  47. if (avpkt->size < avctx->width * 2 * avctx->height + 4 + 2*8) {
  48. av_log(avctx, AV_LOG_ERROR, "Packet is too small.\n");
  49. return AVERROR_INVALIDDATA;
  50. }
  51. if (bytestream_get_le32(&buf) != MKTAG('F', 'R', 'W', '1')) {
  52. av_log(avctx, AV_LOG_ERROR, "incorrect marker\n");
  53. return AVERROR_INVALIDDATA;
  54. }
  55. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  56. return ret;
  57. pic->pict_type = AV_PICTURE_TYPE_I;
  58. pic->key_frame = 1;
  59. for (field = 0; field < 2; field++) {
  60. int i;
  61. int field_h = (avctx->height + !field) >> 1;
  62. int field_size, min_field_size = avctx->width * 2 * field_h;
  63. uint8_t *dst = pic->data[0];
  64. if (buf_end - buf < 8)
  65. return AVERROR_INVALIDDATA;
  66. buf += 4; // flags? 0x80 == bottom field maybe?
  67. field_size = bytestream_get_le32(&buf);
  68. if (field_size < min_field_size) {
  69. av_log(avctx, AV_LOG_ERROR, "Field size %i is too small (required %i)\n", field_size, min_field_size);
  70. return AVERROR_INVALIDDATA;
  71. }
  72. if (buf_end - buf < field_size) {
  73. av_log(avctx, AV_LOG_ERROR, "Packet is too small, need %i, have %i\n", field_size, (int)(buf_end - buf));
  74. return AVERROR_INVALIDDATA;
  75. }
  76. if (field ^ s->change_field_order) {
  77. dst += pic->linesize[0];
  78. } else if (s->change_field_order) {
  79. dst += 2 * pic->linesize[0];
  80. }
  81. for (i = 0; i < field_h; i++) {
  82. if (s->change_field_order && field && i == field_h - 1)
  83. dst = pic->data[0];
  84. memcpy(dst, buf, avctx->width * 2);
  85. buf += avctx->width * 2;
  86. dst += pic->linesize[0] << 1;
  87. }
  88. buf += field_size - min_field_size;
  89. }
  90. *got_frame = 1;
  91. return avpkt->size;
  92. }
  93. static const AVOption frwu_options[] = {
  94. {"change_field_order", "Change field order", offsetof(FRWUContext, change_field_order), FF_OPT_TYPE_INT,
  95. {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM},
  96. {NULL}
  97. };
  98. static const AVClass frwu_class = {
  99. .class_name = "frwu Decoder",
  100. .item_name = av_default_item_name,
  101. .option = frwu_options,
  102. .version = LIBAVUTIL_VERSION_INT,
  103. };
  104. AVCodec ff_frwu_decoder = {
  105. .name = "frwu",
  106. .long_name = NULL_IF_CONFIG_SMALL("Forward Uncompressed"),
  107. .type = AVMEDIA_TYPE_VIDEO,
  108. .id = AV_CODEC_ID_FRWU,
  109. .priv_data_size = sizeof(FRWUContext),
  110. .init = decode_init,
  111. .decode = decode_frame,
  112. .capabilities = CODEC_CAP_DR1,
  113. .priv_class = &frwu_class,
  114. };