rawdec.c 9.2 KB

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