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.

111 lines
3.0KB

  1. /*
  2. * Megalux Frame demuxer
  3. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Megalux Frame demuxer
  24. */
  25. #include "libavcodec/raw.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "avformat.h"
  28. static const PixelFormatTag frm_pix_fmt_tags[] = {
  29. { AV_PIX_FMT_RGB555, 1 },
  30. { AV_PIX_FMT_RGB0, 2 },
  31. { AV_PIX_FMT_RGB24, 3 },
  32. { AV_PIX_FMT_BGR0, 4 },
  33. { AV_PIX_FMT_BGRA, 5 },
  34. { AV_PIX_FMT_NONE, 0 },
  35. };
  36. typedef struct {
  37. int count;
  38. } FrmContext;
  39. static int frm_read_probe(AVProbeData *p)
  40. {
  41. if (p->buf_size > 8 &&
  42. p->buf[0] == 'F' && p->buf[1] == 'R' && p->buf[2] == 'M' &&
  43. AV_RL16(&p->buf[4]) && AV_RL16(&p->buf[6]))
  44. return AVPROBE_SCORE_MAX / 4;
  45. return 0;
  46. }
  47. static int frm_read_header(AVFormatContext *avctx)
  48. {
  49. AVIOContext *pb = avctx->pb;
  50. AVStream *st = avformat_new_stream(avctx, 0);
  51. if (!st)
  52. return AVERROR(ENOMEM);
  53. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  54. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  55. avio_skip(pb, 3);
  56. st->codec->pix_fmt = avpriv_find_pix_fmt(frm_pix_fmt_tags, avio_r8(pb));
  57. if (!st->codec->pix_fmt)
  58. return AVERROR_INVALIDDATA;
  59. st->codec->codec_tag = 0;
  60. st->codec->width = avio_rl16(pb);
  61. st->codec->height = avio_rl16(pb);
  62. return 0;
  63. }
  64. static int frm_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  65. {
  66. FrmContext *s = avctx->priv_data;
  67. AVCodecContext *stc = avctx->streams[0]->codec;
  68. int packet_size, ret;
  69. if (s->count)
  70. return AVERROR_EOF;
  71. packet_size = avpicture_get_size(stc->pix_fmt, stc->width, stc->height);
  72. if (packet_size < 0)
  73. return AVERROR_INVALIDDATA;
  74. ret = av_get_packet(avctx->pb, pkt, packet_size);
  75. if (ret < 0)
  76. return ret;
  77. if (stc->pix_fmt == AV_PIX_FMT_BGRA) {
  78. int i;
  79. for (i = 3; i + 1 <= pkt->size; i += 4)
  80. pkt->data[i] = 0xFF - pkt->data[i];
  81. }
  82. pkt->stream_index = 0;
  83. s->count++;
  84. return 0;
  85. }
  86. AVInputFormat ff_frm_demuxer = {
  87. .name = "frm",
  88. .priv_data_size = sizeof(FrmContext),
  89. .long_name = NULL_IF_CONFIG_SMALL("Megalux Frame"),
  90. .read_probe = frm_read_probe,
  91. .read_header = frm_read_header,
  92. .read_packet = frm_read_packet,
  93. };