rawdec.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "internal.h"
  24. #include "avio_internal.h"
  25. #include "rawdec.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/parseutils.h"
  28. #include "libavutil/pixdesc.h"
  29. /* raw input */
  30. int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
  31. {
  32. AVStream *st;
  33. enum CodecID id;
  34. st = avformat_new_stream(s, NULL);
  35. if (!st)
  36. return AVERROR(ENOMEM);
  37. id = s->iformat->value;
  38. if (id == CODEC_ID_RAWVIDEO) {
  39. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  40. } else {
  41. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  42. }
  43. st->codec->codec_id = id;
  44. switch(st->codec->codec_type) {
  45. case AVMEDIA_TYPE_AUDIO: {
  46. RawAudioDemuxerContext *s1 = s->priv_data;
  47. st->codec->channels = 1;
  48. if (id == CODEC_ID_ADPCM_G722)
  49. st->codec->sample_rate = 16000;
  50. if (s1 && s1->sample_rate)
  51. st->codec->sample_rate = s1->sample_rate;
  52. if (st->codec->sample_rate <= 0) {
  53. av_log(s, AV_LOG_WARNING, "Invalid sample rate %d specified using default of 44100\n",
  54. st->codec->sample_rate);
  55. st->codec->sample_rate= 44100;
  56. }
  57. if (s1 && s1->channels)
  58. st->codec->channels = s1->channels;
  59. st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
  60. assert(st->codec->bits_per_coded_sample > 0);
  61. st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
  62. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  63. break;
  64. }
  65. case AVMEDIA_TYPE_VIDEO: {
  66. FFRawVideoDemuxerContext *s1 = s->priv_data;
  67. int width = 0, height = 0, ret = 0;
  68. enum PixelFormat pix_fmt;
  69. AVRational framerate;
  70. if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
  71. av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
  72. goto fail;
  73. }
  74. if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
  75. av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
  76. ret = AVERROR(EINVAL);
  77. goto fail;
  78. }
  79. if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
  80. av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
  81. goto fail;
  82. }
  83. avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
  84. st->codec->width = width;
  85. st->codec->height = height;
  86. st->codec->pix_fmt = pix_fmt;
  87. fail:
  88. return ret;
  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. av_shrink_packet(pkt, ret);
  110. return ret;
  111. }
  112. int ff_raw_audio_read_header(AVFormatContext *s,
  113. AVFormatParameters *ap)
  114. {
  115. AVStream *st = avformat_new_stream(s, NULL);
  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. FFRawVideoDemuxerContext *s1 = s->priv_data;
  131. AVRational framerate;
  132. int ret = 0;
  133. st = avformat_new_stream(s, NULL);
  134. if (!st) {
  135. ret = AVERROR(ENOMEM);
  136. goto fail;
  137. }
  138. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  139. st->codec->codec_id = s->iformat->value;
  140. st->need_parsing = AVSTREAM_PARSE_FULL;
  141. if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
  142. av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
  143. goto fail;
  144. }
  145. st->codec->time_base = (AVRational){framerate.den, framerate.num};
  146. avpriv_set_pts_info(st, 64, 1, 1200000);
  147. fail:
  148. return ret;
  149. }
  150. /* Note: Do not forget to add new entries to the Makefile as well. */
  151. #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
  152. #define DEC AV_OPT_FLAG_DECODING_PARAM
  153. const AVOption ff_rawvideo_options[] = {
  154. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
  155. { NULL },
  156. };
  157. #if CONFIG_G722_DEMUXER
  158. AVInputFormat ff_g722_demuxer = {
  159. .name = "g722",
  160. .long_name = NULL_IF_CONFIG_SMALL("raw G.722"),
  161. .read_header = ff_raw_read_header,
  162. .read_packet = ff_raw_read_partial_packet,
  163. .flags= AVFMT_GENERIC_INDEX,
  164. .extensions = "g722,722",
  165. .value = CODEC_ID_ADPCM_G722,
  166. };
  167. #endif
  168. #if CONFIG_LATM_DEMUXER
  169. AVInputFormat ff_latm_demuxer = {
  170. .name = "latm",
  171. .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
  172. .read_header = ff_raw_audio_read_header,
  173. .read_packet = ff_raw_read_partial_packet,
  174. .flags= AVFMT_GENERIC_INDEX,
  175. .extensions = "latm",
  176. .value = CODEC_ID_AAC_LATM,
  177. };
  178. #endif
  179. #if CONFIG_MJPEG_DEMUXER
  180. FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg,mpo", CODEC_ID_MJPEG)
  181. #endif
  182. #if CONFIG_MLP_DEMUXER
  183. AVInputFormat ff_mlp_demuxer = {
  184. .name = "mlp",
  185. .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
  186. .read_header = ff_raw_audio_read_header,
  187. .read_packet = ff_raw_read_partial_packet,
  188. .flags= AVFMT_GENERIC_INDEX,
  189. .extensions = "mlp",
  190. .value = CODEC_ID_MLP,
  191. };
  192. #endif
  193. #if CONFIG_TRUEHD_DEMUXER
  194. AVInputFormat ff_truehd_demuxer = {
  195. .name = "truehd",
  196. .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
  197. .read_header = ff_raw_audio_read_header,
  198. .read_packet = ff_raw_read_partial_packet,
  199. .flags= AVFMT_GENERIC_INDEX,
  200. .extensions = "thd",
  201. .value = CODEC_ID_TRUEHD,
  202. };
  203. #endif
  204. #if CONFIG_SHORTEN_DEMUXER
  205. AVInputFormat ff_shorten_demuxer = {
  206. .name = "shn",
  207. .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
  208. .read_header = ff_raw_audio_read_header,
  209. .read_packet = ff_raw_read_partial_packet,
  210. .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
  211. .extensions = "shn",
  212. .value = CODEC_ID_SHORTEN,
  213. };
  214. #endif
  215. #if CONFIG_VC1_DEMUXER
  216. FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
  217. #endif