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.2KB

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