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.

114 lines
3.8KB

  1. /*
  2. * RAW video demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  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. #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. int width, height; /**< Integers describing video size, set by a private option. */
  30. char *pixel_format; /**< Set by a private option. */
  31. AVRational framerate; /**< AVRational 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. enum AVPixelFormat pix_fmt;
  37. AVStream *st;
  38. int packet_size;
  39. st = avformat_new_stream(ctx, NULL);
  40. if (!st)
  41. return AVERROR(ENOMEM);
  42. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  43. st->codecpar->codec_id = ctx->iformat->raw_codec_id;
  44. if ((pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
  45. av_log(ctx, AV_LOG_ERROR, "No such pixel format: %s.\n",
  46. s->pixel_format);
  47. return AVERROR(EINVAL);
  48. }
  49. avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
  50. st->codecpar->width = s->width;
  51. st->codecpar->height = s->height;
  52. st->codecpar->format = pix_fmt;
  53. packet_size = av_image_get_buffer_size(st->codecpar->format, s->width, s->height, 1);
  54. if (packet_size < 0)
  55. return packet_size;
  56. ctx->packet_size = packet_size;
  57. st->codecpar->bit_rate = av_rescale_q(ctx->packet_size,
  58. (AVRational){8,1}, st->time_base);
  59. return 0;
  60. }
  61. static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
  62. {
  63. int ret;
  64. ret = av_get_packet(s->pb, pkt, s->packet_size);
  65. pkt->pts = pkt->dts = pkt->pos / s->packet_size;
  66. pkt->stream_index = 0;
  67. if (ret < 0)
  68. return ret;
  69. return 0;
  70. }
  71. #define OFFSET(x) offsetof(RawVideoDemuxerContext, x)
  72. #define DEC AV_OPT_FLAG_DECODING_PARAM
  73. static const AVOption rawvideo_options[] = {
  74. { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  75. { "pixel_format", "set pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
  76. { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
  77. { NULL },
  78. };
  79. static const AVClass rawvideo_demuxer_class = {
  80. .class_name = "rawvideo demuxer",
  81. .item_name = av_default_item_name,
  82. .option = rawvideo_options,
  83. .version = LIBAVUTIL_VERSION_INT,
  84. };
  85. AVInputFormat ff_rawvideo_demuxer = {
  86. .name = "rawvideo",
  87. .long_name = NULL_IF_CONFIG_SMALL("raw video"),
  88. .priv_data_size = sizeof(RawVideoDemuxerContext),
  89. .read_header = rawvideo_read_header,
  90. .read_packet = rawvideo_read_packet,
  91. .flags = AVFMT_GENERIC_INDEX,
  92. .extensions = "yuv,cif,qcif,rgb",
  93. .raw_codec_id = AV_CODEC_ID_RAWVIDEO,
  94. .priv_class = &rawvideo_demuxer_class,
  95. };