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.

266 lines
7.3KB

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