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

  1. /*
  2. * Raw v210 video demuxer
  3. * Copyright (c) 2015 Tiancheng "Timothy" Gu
  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 V210DemuxerContext {
  28. const AVClass *class; /**< Class for private options. */
  29. int width, height; /**< Integers describing video size, set by a private option. */
  30. AVRational framerate; /**< AVRational describing framerate, set by a private option. */
  31. } V210DemuxerContext;
  32. // v210 frame width is padded to multiples of 48
  33. #define GET_PACKET_SIZE(w, h) (((w + 47) / 48) * 48 * h * 8 / 3)
  34. static int v210_read_header(AVFormatContext *ctx)
  35. {
  36. V210DemuxerContext *s = ctx->priv_data;
  37. AVStream *st;
  38. int ret;
  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. avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
  45. ret = av_image_check_size(s->width, s->height, 0, ctx);
  46. if (ret < 0)
  47. return ret;
  48. st->codecpar->width = s->width;
  49. st->codecpar->height = s->height;
  50. st->codecpar->format = ctx->iformat->raw_codec_id == AV_CODEC_ID_V210 ?
  51. AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV422P16;
  52. ctx->packet_size = GET_PACKET_SIZE(s->width, s->height);
  53. st->codecpar->bit_rate = av_rescale_q(ctx->packet_size,
  54. (AVRational){8,1}, st->time_base);
  55. return 0;
  56. }
  57. static int v210_read_packet(AVFormatContext *s, AVPacket *pkt)
  58. {
  59. int ret;
  60. ret = av_get_packet(s->pb, pkt, s->packet_size);
  61. pkt->pts = pkt->dts = pkt->pos / s->packet_size;
  62. pkt->stream_index = 0;
  63. if (ret < 0)
  64. return ret;
  65. return 0;
  66. }
  67. #define OFFSET(x) offsetof(V210DemuxerContext, x)
  68. #define DEC AV_OPT_FLAG_DECODING_PARAM
  69. static const AVOption v210_options[] = {
  70. { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  71. { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
  72. { NULL },
  73. };
  74. #if CONFIG_V210_DEMUXER
  75. static const AVClass v210_demuxer_class = {
  76. .class_name = "v210 demuxer",
  77. .item_name = av_default_item_name,
  78. .option = v210_options,
  79. .version = LIBAVUTIL_VERSION_INT,
  80. };
  81. AVInputFormat ff_v210_demuxer = {
  82. .name = "v210",
  83. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  84. .priv_data_size = sizeof(V210DemuxerContext),
  85. .read_header = v210_read_header,
  86. .read_packet = v210_read_packet,
  87. .flags = AVFMT_GENERIC_INDEX,
  88. .extensions = "v210",
  89. .raw_codec_id = AV_CODEC_ID_V210,
  90. .priv_class = &v210_demuxer_class,
  91. };
  92. #endif // CONFIG_V210_DEMUXER
  93. #if CONFIG_V210X_DEMUXER
  94. static const AVClass v210x_demuxer_class = {
  95. .class_name = "v210x demuxer",
  96. .item_name = av_default_item_name,
  97. .option = v210_options,
  98. .version = LIBAVUTIL_VERSION_INT,
  99. };
  100. AVInputFormat ff_v210x_demuxer = {
  101. .name = "v210x",
  102. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  103. .priv_data_size = sizeof(V210DemuxerContext),
  104. .read_header = v210_read_header,
  105. .read_packet = v210_read_packet,
  106. .flags = AVFMT_GENERIC_INDEX,
  107. .extensions = "yuv10",
  108. .raw_codec_id = AV_CODEC_ID_V210X,
  109. .priv_class = &v210x_demuxer_class,
  110. };
  111. #endif // CONFIG_V210X_DEMUXER