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.

134 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. st = avformat_new_stream(ctx, NULL);
  39. if (!st)
  40. return AVERROR(ENOMEM);
  41. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  42. st->codec->codec_id = ctx->iformat->raw_codec_id;
  43. avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
  44. st->codec->width = s->width;
  45. st->codec->height = s->height;
  46. st->codec->pix_fmt = ctx->iformat->raw_codec_id == AV_CODEC_ID_V210 ?
  47. AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV422P16;
  48. st->codec->bit_rate = av_rescale_q(GET_PACKET_SIZE(s->width, s->height),
  49. (AVRational){8,1}, st->time_base);
  50. return 0;
  51. }
  52. static int v210_read_packet(AVFormatContext *s, AVPacket *pkt)
  53. {
  54. int packet_size, ret, width, height;
  55. AVStream *st = s->streams[0];
  56. width = st->codec->width;
  57. height = st->codec->height;
  58. packet_size = GET_PACKET_SIZE(width, height);
  59. if (packet_size < 0)
  60. return -1;
  61. ret = av_get_packet(s->pb, pkt, packet_size);
  62. pkt->pts = pkt->dts = pkt->pos / packet_size;
  63. pkt->stream_index = 0;
  64. if (ret < 0)
  65. return ret;
  66. return 0;
  67. }
  68. #define OFFSET(x) offsetof(V210DemuxerContext, x)
  69. #define DEC AV_OPT_FLAG_DECODING_PARAM
  70. static const AVOption v210_options[] = {
  71. { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  72. { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
  73. { NULL },
  74. };
  75. #if CONFIG_V210_DEMUXER
  76. static const AVClass v210_demuxer_class = {
  77. .class_name = "v210 demuxer",
  78. .item_name = av_default_item_name,
  79. .option = v210_options,
  80. .version = LIBAVUTIL_VERSION_INT,
  81. };
  82. AVInputFormat ff_v210_demuxer = {
  83. .name = "v210",
  84. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  85. .priv_data_size = sizeof(V210DemuxerContext),
  86. .read_header = v210_read_header,
  87. .read_packet = v210_read_packet,
  88. .flags = AVFMT_GENERIC_INDEX,
  89. .extensions = "v210",
  90. .raw_codec_id = AV_CODEC_ID_V210,
  91. .priv_class = &v210_demuxer_class,
  92. };
  93. #endif // CONFIG_V210_DEMUXER
  94. #if CONFIG_V210X_DEMUXER
  95. static const AVClass v210x_demuxer_class = {
  96. .class_name = "v210x demuxer",
  97. .item_name = av_default_item_name,
  98. .option = v210_options,
  99. .version = LIBAVUTIL_VERSION_INT,
  100. };
  101. AVInputFormat ff_v210x_demuxer = {
  102. .name = "v210x",
  103. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  104. .priv_data_size = sizeof(V210DemuxerContext),
  105. .read_header = v210_read_header,
  106. .read_packet = v210_read_packet,
  107. .flags = AVFMT_GENERIC_INDEX,
  108. .extensions = "yuv10",
  109. .raw_codec_id = AV_CODEC_ID_V210X,
  110. .priv_class = &v210x_demuxer_class,
  111. };
  112. #endif // CONFIG_V210X_DEMUXER