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.

128 lines
4.1KB

  1. /*
  2. * RAW video demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/parseutils.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/opt.h"
  24. #include "internal.h"
  25. #include "avformat.h"
  26. typedef struct RawVideoDemuxerContext {
  27. const AVClass *class; /**< Class for private options. */
  28. char *video_size; /**< String describing video size, set by a private option. */
  29. char *pixel_format; /**< Set by a private option. */
  30. char *framerate; /**< String describing framerate, set by a private option. */
  31. } RawVideoDemuxerContext;
  32. static int rawvideo_read_header(AVFormatContext *ctx)
  33. {
  34. RawVideoDemuxerContext *s = ctx->priv_data;
  35. int width = 0, height = 0, ret = 0;
  36. enum AVPixelFormat pix_fmt;
  37. AVRational framerate;
  38. AVStream *st;
  39. st = avformat_new_stream(ctx, NULL);
  40. if (!st)
  41. return AVERROR(ENOMEM);
  42. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  43. st->codec->codec_id = ctx->iformat->raw_codec_id;
  44. if (s->video_size &&
  45. (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
  46. av_log(ctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
  47. return ret;
  48. }
  49. if ((pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
  50. av_log(ctx, AV_LOG_ERROR, "No such pixel format: %s.\n",
  51. s->pixel_format);
  52. return AVERROR(EINVAL);
  53. }
  54. if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
  55. av_log(ctx, AV_LOG_ERROR, "Could not parse framerate: %s.\n",
  56. s->framerate);
  57. return ret;
  58. }
  59. avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
  60. st->codec->width = width;
  61. st->codec->height = height;
  62. st->codec->pix_fmt = pix_fmt;
  63. return 0;
  64. }
  65. static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
  66. {
  67. int packet_size, ret, width, height;
  68. AVStream *st = s->streams[0];
  69. width = st->codec->width;
  70. height = st->codec->height;
  71. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  72. if (packet_size < 0)
  73. return -1;
  74. ret = av_get_packet(s->pb, pkt, packet_size);
  75. pkt->pts = pkt->dts = pkt->pos / packet_size;
  76. pkt->stream_index = 0;
  77. if (ret < 0)
  78. return ret;
  79. return 0;
  80. }
  81. #define OFFSET(x) offsetof(RawVideoDemuxerContext, x)
  82. #define DEC AV_OPT_FLAG_DECODING_PARAM
  83. static const AVOption rawvideo_options[] = {
  84. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  85. { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
  86. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  87. { NULL },
  88. };
  89. static const AVClass rawvideo_demuxer_class = {
  90. .class_name = "rawvideo demuxer",
  91. .item_name = av_default_item_name,
  92. .option = rawvideo_options,
  93. .version = LIBAVUTIL_VERSION_INT,
  94. };
  95. AVInputFormat ff_rawvideo_demuxer = {
  96. .name = "rawvideo",
  97. .long_name = NULL_IF_CONFIG_SMALL("raw video"),
  98. .priv_data_size = sizeof(RawVideoDemuxerContext),
  99. .read_header = rawvideo_read_header,
  100. .read_packet = rawvideo_read_packet,
  101. .flags = AVFMT_GENERIC_INDEX,
  102. .extensions = "yuv,cif,qcif,rgb",
  103. .raw_codec_id = AV_CODEC_ID_RAWVIDEO,
  104. .priv_class = &rawvideo_demuxer_class,
  105. };