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.

288 lines
8.4KB

  1. /*
  2. * RAW demuxers
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2005 Alex Beregszaszi
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avformat.h"
  23. #include "avio_internal.h"
  24. #include "rawdec.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/parseutils.h"
  27. #include "libavutil/pixdesc.h"
  28. /* raw input */
  29. int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
  30. {
  31. AVStream *st;
  32. enum CodecID id;
  33. st = av_new_stream(s, 0);
  34. if (!st)
  35. return AVERROR(ENOMEM);
  36. id = s->iformat->value;
  37. if (id == CODEC_ID_RAWVIDEO) {
  38. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  39. } else {
  40. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  41. }
  42. st->codec->codec_id = id;
  43. switch(st->codec->codec_type) {
  44. case AVMEDIA_TYPE_AUDIO: {
  45. RawAudioDemuxerContext *s1 = s->priv_data;
  46. #if FF_API_FORMAT_PARAMETERS
  47. if (ap->sample_rate)
  48. st->codec->sample_rate = ap->sample_rate;
  49. if (ap->channels)
  50. st->codec->channels = ap->channels;
  51. else st->codec->channels = 1;
  52. #endif
  53. if (s1->sample_rate)
  54. st->codec->sample_rate = s1->sample_rate;
  55. if (s1->channels)
  56. st->codec->channels = s1->channels;
  57. st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
  58. assert(st->codec->bits_per_coded_sample > 0);
  59. st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
  60. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  61. break;
  62. }
  63. case AVMEDIA_TYPE_VIDEO: {
  64. FFRawVideoDemuxerContext *s1 = s->priv_data;
  65. int width = 0, height = 0, ret;
  66. enum PixelFormat pix_fmt;
  67. if(ap->time_base.num)
  68. av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
  69. else
  70. av_set_pts_info(st, 64, 1, 25);
  71. if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
  72. av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
  73. goto fail;
  74. }
  75. if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
  76. av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
  77. ret = AVERROR(EINVAL);
  78. goto fail;
  79. }
  80. #if FF_API_FORMAT_PARAMETERS
  81. if (ap->width > 0)
  82. width = ap->width;
  83. if (ap->height > 0)
  84. height = ap->height;
  85. if (ap->pix_fmt)
  86. pix_fmt = ap->pix_fmt;
  87. #endif
  88. st->codec->width = width;
  89. st->codec->height = height;
  90. st->codec->pix_fmt = pix_fmt;
  91. fail:
  92. av_freep(&s1->video_size);
  93. av_freep(&s1->pixel_format);
  94. return ret;
  95. }
  96. default:
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. #define RAW_PACKET_SIZE 1024
  102. int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
  103. {
  104. int ret, size;
  105. size = RAW_PACKET_SIZE;
  106. if (av_new_packet(pkt, size) < 0)
  107. return AVERROR(ENOMEM);
  108. pkt->pos= avio_tell(s->pb);
  109. pkt->stream_index = 0;
  110. ret = ffio_read_partial(s->pb, pkt->data, size);
  111. if (ret < 0) {
  112. av_free_packet(pkt);
  113. return ret;
  114. }
  115. pkt->size = ret;
  116. return ret;
  117. }
  118. int ff_raw_audio_read_header(AVFormatContext *s,
  119. AVFormatParameters *ap)
  120. {
  121. AVStream *st = av_new_stream(s, 0);
  122. if (!st)
  123. return AVERROR(ENOMEM);
  124. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  125. st->codec->codec_id = s->iformat->value;
  126. st->need_parsing = AVSTREAM_PARSE_FULL;
  127. /* the parameters will be extracted from the compressed bitstream */
  128. return 0;
  129. }
  130. /* MPEG-1/H.263 input */
  131. int ff_raw_video_read_header(AVFormatContext *s,
  132. AVFormatParameters *ap)
  133. {
  134. AVStream *st;
  135. st = av_new_stream(s, 0);
  136. if (!st)
  137. return AVERROR(ENOMEM);
  138. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  139. st->codec->codec_id = s->iformat->value;
  140. st->need_parsing = AVSTREAM_PARSE_FULL;
  141. /* for MJPEG, specify frame rate */
  142. /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
  143. if (ap->time_base.num) {
  144. st->codec->time_base= ap->time_base;
  145. } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
  146. st->codec->codec_id == CODEC_ID_MPEG4 ||
  147. st->codec->codec_id == CODEC_ID_DIRAC ||
  148. st->codec->codec_id == CODEC_ID_DNXHD ||
  149. st->codec->codec_id == CODEC_ID_VC1 ||
  150. st->codec->codec_id == CODEC_ID_H264) {
  151. st->codec->time_base= (AVRational){1,25};
  152. }
  153. av_set_pts_info(st, 64, 1, 1200000);
  154. return 0;
  155. }
  156. /* Note: Do not forget to add new entries to the Makefile as well. */
  157. static const AVOption audio_options[] = {
  158. { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  159. { "channels", "", offsetof(RawAudioDemuxerContext, channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  160. { NULL },
  161. };
  162. const AVClass ff_rawaudio_demuxer_class = {
  163. .class_name = "rawaudio demuxer",
  164. .item_name = av_default_item_name,
  165. .option = audio_options,
  166. .version = LIBAVUTIL_VERSION_INT,
  167. };
  168. #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
  169. #define DEC AV_OPT_FLAG_DECODING_PARAM
  170. static const AVOption video_options[] = {
  171. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  172. { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
  173. { NULL },
  174. };
  175. #undef OFFSET
  176. #undef DEC
  177. const AVClass ff_rawvideo_demuxer_class = {
  178. .class_name = "rawvideo demuxer",
  179. .item_name = av_default_item_name,
  180. .option = video_options,
  181. .version = LIBAVUTIL_VERSION_INT,
  182. };
  183. #if CONFIG_G722_DEMUXER
  184. AVInputFormat ff_g722_demuxer = {
  185. "g722",
  186. NULL_IF_CONFIG_SMALL("raw G.722"),
  187. sizeof(RawAudioDemuxerContext),
  188. NULL,
  189. ff_raw_read_header,
  190. ff_raw_read_partial_packet,
  191. .flags= AVFMT_GENERIC_INDEX,
  192. .extensions = "g722,722",
  193. .value = CODEC_ID_ADPCM_G722,
  194. .priv_class = &ff_rawaudio_demuxer_class,
  195. };
  196. #endif
  197. #if CONFIG_GSM_DEMUXER
  198. AVInputFormat ff_gsm_demuxer = {
  199. "gsm",
  200. NULL_IF_CONFIG_SMALL("raw GSM"),
  201. 0,
  202. NULL,
  203. ff_raw_audio_read_header,
  204. ff_raw_read_partial_packet,
  205. .flags= AVFMT_GENERIC_INDEX,
  206. .extensions = "gsm",
  207. .value = CODEC_ID_GSM,
  208. };
  209. #endif
  210. #if CONFIG_MJPEG_DEMUXER
  211. FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
  212. #endif
  213. #if CONFIG_MLP_DEMUXER
  214. AVInputFormat ff_mlp_demuxer = {
  215. "mlp",
  216. NULL_IF_CONFIG_SMALL("raw MLP"),
  217. 0,
  218. NULL,
  219. ff_raw_audio_read_header,
  220. ff_raw_read_partial_packet,
  221. .flags= AVFMT_GENERIC_INDEX,
  222. .extensions = "mlp",
  223. .value = CODEC_ID_MLP,
  224. };
  225. #endif
  226. #if CONFIG_TRUEHD_DEMUXER
  227. AVInputFormat ff_truehd_demuxer = {
  228. "truehd",
  229. NULL_IF_CONFIG_SMALL("raw TrueHD"),
  230. 0,
  231. NULL,
  232. ff_raw_audio_read_header,
  233. ff_raw_read_partial_packet,
  234. .flags= AVFMT_GENERIC_INDEX,
  235. .extensions = "thd",
  236. .value = CODEC_ID_TRUEHD,
  237. };
  238. #endif
  239. #if CONFIG_SHORTEN_DEMUXER
  240. AVInputFormat ff_shorten_demuxer = {
  241. "shn",
  242. NULL_IF_CONFIG_SMALL("raw Shorten"),
  243. 0,
  244. NULL,
  245. ff_raw_audio_read_header,
  246. ff_raw_read_partial_packet,
  247. .flags= AVFMT_GENERIC_INDEX,
  248. .extensions = "shn",
  249. .value = CODEC_ID_SHORTEN,
  250. };
  251. #endif
  252. #if CONFIG_VC1_DEMUXER
  253. FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
  254. #endif