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.

280 lines
8.1KB

  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. /* raw input */
  28. int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
  29. {
  30. AVStream *st;
  31. enum CodecID id;
  32. st = av_new_stream(s, 0);
  33. if (!st)
  34. return AVERROR(ENOMEM);
  35. id = s->iformat->value;
  36. if (id == CODEC_ID_RAWVIDEO) {
  37. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  38. } else {
  39. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  40. }
  41. st->codec->codec_id = id;
  42. switch(st->codec->codec_type) {
  43. case AVMEDIA_TYPE_AUDIO: {
  44. RawAudioDemuxerContext *s1 = s->priv_data;
  45. #if FF_API_FORMAT_PARAMETERS
  46. if (ap->sample_rate)
  47. st->codec->sample_rate = ap->sample_rate;
  48. if (ap->channels)
  49. st->codec->channels = ap->channels;
  50. else st->codec->channels = 1;
  51. #endif
  52. if (s1->sample_rate)
  53. st->codec->sample_rate = s1->sample_rate;
  54. if (s1->channels)
  55. st->codec->channels = s1->channels;
  56. st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
  57. assert(st->codec->bits_per_coded_sample > 0);
  58. st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
  59. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  60. break;
  61. }
  62. case AVMEDIA_TYPE_VIDEO: {
  63. FFRawVideoDemuxerContext *s1 = s->priv_data;
  64. int width = 0, height = 0, ret;
  65. if(ap->time_base.num)
  66. av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
  67. else
  68. av_set_pts_info(st, 64, 1, 25);
  69. if (s1->video_size) {
  70. ret = av_parse_video_size(&width, &height, s1->video_size);
  71. av_freep(&s1->video_size);
  72. if (ret < 0) {
  73. av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
  74. return ret;
  75. }
  76. }
  77. #if FF_API_FORMAT_PARAMETERS
  78. if (ap->width > 0)
  79. width = ap->width;
  80. if (ap->height > 0)
  81. height = ap->height;
  82. #endif
  83. st->codec->width = width;
  84. st->codec->height = height;
  85. st->codec->pix_fmt = ap->pix_fmt;
  86. if(st->codec->pix_fmt == PIX_FMT_NONE)
  87. st->codec->pix_fmt= PIX_FMT_YUV420P;
  88. break;
  89. }
  90. default:
  91. return -1;
  92. }
  93. return 0;
  94. }
  95. #define RAW_PACKET_SIZE 1024
  96. int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
  97. {
  98. int ret, size;
  99. size = RAW_PACKET_SIZE;
  100. if (av_new_packet(pkt, size) < 0)
  101. return AVERROR(ENOMEM);
  102. pkt->pos= avio_tell(s->pb);
  103. pkt->stream_index = 0;
  104. ret = ffio_read_partial(s->pb, pkt->data, size);
  105. if (ret < 0) {
  106. av_free_packet(pkt);
  107. return ret;
  108. }
  109. pkt->size = ret;
  110. return ret;
  111. }
  112. int ff_raw_audio_read_header(AVFormatContext *s,
  113. AVFormatParameters *ap)
  114. {
  115. AVStream *st = av_new_stream(s, 0);
  116. if (!st)
  117. return AVERROR(ENOMEM);
  118. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  119. st->codec->codec_id = s->iformat->value;
  120. st->need_parsing = AVSTREAM_PARSE_FULL;
  121. /* the parameters will be extracted from the compressed bitstream */
  122. return 0;
  123. }
  124. /* MPEG-1/H.263 input */
  125. int ff_raw_video_read_header(AVFormatContext *s,
  126. AVFormatParameters *ap)
  127. {
  128. AVStream *st;
  129. st = av_new_stream(s, 0);
  130. if (!st)
  131. return AVERROR(ENOMEM);
  132. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  133. st->codec->codec_id = s->iformat->value;
  134. st->need_parsing = AVSTREAM_PARSE_FULL;
  135. /* for MJPEG, specify frame rate */
  136. /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
  137. if (ap->time_base.num) {
  138. st->codec->time_base= ap->time_base;
  139. } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
  140. st->codec->codec_id == CODEC_ID_MPEG4 ||
  141. st->codec->codec_id == CODEC_ID_DIRAC ||
  142. st->codec->codec_id == CODEC_ID_DNXHD ||
  143. st->codec->codec_id == CODEC_ID_VC1 ||
  144. st->codec->codec_id == CODEC_ID_H264) {
  145. st->codec->time_base= (AVRational){1,25};
  146. }
  147. av_set_pts_info(st, 64, 1, 1200000);
  148. return 0;
  149. }
  150. /* Note: Do not forget to add new entries to the Makefile as well. */
  151. static const AVOption audio_options[] = {
  152. { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  153. { "channels", "", offsetof(RawAudioDemuxerContext, channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  154. { NULL },
  155. };
  156. const AVClass ff_rawaudio_demuxer_class = {
  157. .class_name = "rawaudio demuxer",
  158. .item_name = av_default_item_name,
  159. .option = audio_options,
  160. .version = LIBAVUTIL_VERSION_INT,
  161. };
  162. #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
  163. #define DEC AV_OPT_FLAG_DECODING_PARAM
  164. static const AVOption video_options[] = {
  165. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  166. { NULL },
  167. };
  168. #undef OFFSET
  169. #undef DEC
  170. const AVClass ff_rawvideo_demuxer_class = {
  171. .class_name = "rawvideo demuxer",
  172. .item_name = av_default_item_name,
  173. .option = video_options,
  174. .version = LIBAVUTIL_VERSION_INT,
  175. };
  176. #if CONFIG_G722_DEMUXER
  177. AVInputFormat ff_g722_demuxer = {
  178. "g722",
  179. NULL_IF_CONFIG_SMALL("raw G.722"),
  180. sizeof(RawAudioDemuxerContext),
  181. NULL,
  182. ff_raw_read_header,
  183. ff_raw_read_partial_packet,
  184. .flags= AVFMT_GENERIC_INDEX,
  185. .extensions = "g722,722",
  186. .value = CODEC_ID_ADPCM_G722,
  187. .priv_class = &ff_rawaudio_demuxer_class,
  188. };
  189. #endif
  190. #if CONFIG_GSM_DEMUXER
  191. AVInputFormat ff_gsm_demuxer = {
  192. "gsm",
  193. NULL_IF_CONFIG_SMALL("raw GSM"),
  194. 0,
  195. NULL,
  196. ff_raw_audio_read_header,
  197. ff_raw_read_partial_packet,
  198. .flags= AVFMT_GENERIC_INDEX,
  199. .extensions = "gsm",
  200. .value = CODEC_ID_GSM,
  201. };
  202. #endif
  203. #if CONFIG_MJPEG_DEMUXER
  204. FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
  205. #endif
  206. #if CONFIG_MLP_DEMUXER
  207. AVInputFormat ff_mlp_demuxer = {
  208. "mlp",
  209. NULL_IF_CONFIG_SMALL("raw MLP"),
  210. 0,
  211. NULL,
  212. ff_raw_audio_read_header,
  213. ff_raw_read_partial_packet,
  214. .flags= AVFMT_GENERIC_INDEX,
  215. .extensions = "mlp",
  216. .value = CODEC_ID_MLP,
  217. };
  218. #endif
  219. #if CONFIG_TRUEHD_DEMUXER
  220. AVInputFormat ff_truehd_demuxer = {
  221. "truehd",
  222. NULL_IF_CONFIG_SMALL("raw TrueHD"),
  223. 0,
  224. NULL,
  225. ff_raw_audio_read_header,
  226. ff_raw_read_partial_packet,
  227. .flags= AVFMT_GENERIC_INDEX,
  228. .extensions = "thd",
  229. .value = CODEC_ID_TRUEHD,
  230. };
  231. #endif
  232. #if CONFIG_SHORTEN_DEMUXER
  233. AVInputFormat ff_shorten_demuxer = {
  234. "shn",
  235. NULL_IF_CONFIG_SMALL("raw Shorten"),
  236. 0,
  237. NULL,
  238. ff_raw_audio_read_header,
  239. ff_raw_read_partial_packet,
  240. .flags= AVFMT_GENERIC_INDEX,
  241. .extensions = "shn",
  242. .value = CODEC_ID_SHORTEN,
  243. };
  244. #endif
  245. #if CONFIG_VC1_DEMUXER
  246. FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
  247. #endif