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.

289 lines
8.5KB

  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. break;
  92. fail:
  93. av_freep(&s1->video_size);
  94. av_freep(&s1->pixel_format);
  95. return ret;
  96. }
  97. default:
  98. return -1;
  99. }
  100. return 0;
  101. }
  102. #define RAW_PACKET_SIZE 1024
  103. int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
  104. {
  105. int ret, size;
  106. size = RAW_PACKET_SIZE;
  107. if (av_new_packet(pkt, size) < 0)
  108. return AVERROR(ENOMEM);
  109. pkt->pos= avio_tell(s->pb);
  110. pkt->stream_index = 0;
  111. ret = ffio_read_partial(s->pb, pkt->data, size);
  112. if (ret < 0) {
  113. av_free_packet(pkt);
  114. return ret;
  115. }
  116. pkt->size = ret;
  117. return ret;
  118. }
  119. int ff_raw_audio_read_header(AVFormatContext *s,
  120. AVFormatParameters *ap)
  121. {
  122. AVStream *st = av_new_stream(s, 0);
  123. if (!st)
  124. return AVERROR(ENOMEM);
  125. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  126. st->codec->codec_id = s->iformat->value;
  127. st->need_parsing = AVSTREAM_PARSE_FULL;
  128. /* the parameters will be extracted from the compressed bitstream */
  129. return 0;
  130. }
  131. /* MPEG-1/H.263 input */
  132. int ff_raw_video_read_header(AVFormatContext *s,
  133. AVFormatParameters *ap)
  134. {
  135. AVStream *st;
  136. st = av_new_stream(s, 0);
  137. if (!st)
  138. return AVERROR(ENOMEM);
  139. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  140. st->codec->codec_id = s->iformat->value;
  141. st->need_parsing = AVSTREAM_PARSE_FULL;
  142. /* for MJPEG, specify frame rate */
  143. /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
  144. if (ap->time_base.num) {
  145. st->codec->time_base= ap->time_base;
  146. } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
  147. st->codec->codec_id == CODEC_ID_MPEG4 ||
  148. st->codec->codec_id == CODEC_ID_DIRAC ||
  149. st->codec->codec_id == CODEC_ID_DNXHD ||
  150. st->codec->codec_id == CODEC_ID_VC1 ||
  151. st->codec->codec_id == CODEC_ID_H264) {
  152. st->codec->time_base= (AVRational){1,25};
  153. }
  154. av_set_pts_info(st, 64, 1, 1200000);
  155. return 0;
  156. }
  157. /* Note: Do not forget to add new entries to the Makefile as well. */
  158. static const AVOption audio_options[] = {
  159. { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  160. { "channels", "", offsetof(RawAudioDemuxerContext, channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  161. { NULL },
  162. };
  163. const AVClass ff_rawaudio_demuxer_class = {
  164. .class_name = "rawaudio demuxer",
  165. .item_name = av_default_item_name,
  166. .option = audio_options,
  167. .version = LIBAVUTIL_VERSION_INT,
  168. };
  169. #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
  170. #define DEC AV_OPT_FLAG_DECODING_PARAM
  171. static const AVOption video_options[] = {
  172. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  173. { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
  174. { NULL },
  175. };
  176. #undef OFFSET
  177. #undef DEC
  178. const AVClass ff_rawvideo_demuxer_class = {
  179. .class_name = "rawvideo demuxer",
  180. .item_name = av_default_item_name,
  181. .option = video_options,
  182. .version = LIBAVUTIL_VERSION_INT,
  183. };
  184. #if CONFIG_G722_DEMUXER
  185. AVInputFormat ff_g722_demuxer = {
  186. "g722",
  187. NULL_IF_CONFIG_SMALL("raw G.722"),
  188. sizeof(RawAudioDemuxerContext),
  189. NULL,
  190. ff_raw_read_header,
  191. ff_raw_read_partial_packet,
  192. .flags= AVFMT_GENERIC_INDEX,
  193. .extensions = "g722,722",
  194. .value = CODEC_ID_ADPCM_G722,
  195. .priv_class = &ff_rawaudio_demuxer_class,
  196. };
  197. #endif
  198. #if CONFIG_GSM_DEMUXER
  199. AVInputFormat ff_gsm_demuxer = {
  200. "gsm",
  201. NULL_IF_CONFIG_SMALL("raw GSM"),
  202. 0,
  203. NULL,
  204. ff_raw_audio_read_header,
  205. ff_raw_read_partial_packet,
  206. .flags= AVFMT_GENERIC_INDEX,
  207. .extensions = "gsm",
  208. .value = CODEC_ID_GSM,
  209. };
  210. #endif
  211. #if CONFIG_MJPEG_DEMUXER
  212. FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
  213. #endif
  214. #if CONFIG_MLP_DEMUXER
  215. AVInputFormat ff_mlp_demuxer = {
  216. "mlp",
  217. NULL_IF_CONFIG_SMALL("raw MLP"),
  218. 0,
  219. NULL,
  220. ff_raw_audio_read_header,
  221. ff_raw_read_partial_packet,
  222. .flags= AVFMT_GENERIC_INDEX,
  223. .extensions = "mlp",
  224. .value = CODEC_ID_MLP,
  225. };
  226. #endif
  227. #if CONFIG_TRUEHD_DEMUXER
  228. AVInputFormat ff_truehd_demuxer = {
  229. "truehd",
  230. NULL_IF_CONFIG_SMALL("raw TrueHD"),
  231. 0,
  232. NULL,
  233. ff_raw_audio_read_header,
  234. ff_raw_read_partial_packet,
  235. .flags= AVFMT_GENERIC_INDEX,
  236. .extensions = "thd",
  237. .value = CODEC_ID_TRUEHD,
  238. };
  239. #endif
  240. #if CONFIG_SHORTEN_DEMUXER
  241. AVInputFormat ff_shorten_demuxer = {
  242. "shn",
  243. NULL_IF_CONFIG_SMALL("raw Shorten"),
  244. 0,
  245. NULL,
  246. ff_raw_audio_read_header,
  247. ff_raw_read_partial_packet,
  248. .flags= AVFMT_GENERIC_INDEX,
  249. .extensions = "shn",
  250. .value = CODEC_ID_SHORTEN,
  251. };
  252. #endif
  253. #if CONFIG_VC1_DEMUXER
  254. FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
  255. #endif