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.

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