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.

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