rawdec.c 6.3 KB

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