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.

131 lines
4.2KB

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