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.

217 lines
6.9KB

  1. /**
  2. Copyright (C) 2005 Michael Ahlberg, Måns Rullgård
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  16. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  17. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. DEALINGS IN THE SOFTWARE.
  20. **/
  21. #include <stdlib.h>
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavcodec/get_bits.h"
  25. #include "libavcodec/bytestream.h"
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "oggdec.h"
  29. #include "riff.h"
  30. static int
  31. ogm_header(AVFormatContext *s, int idx)
  32. {
  33. struct ogg *ogg = s->priv_data;
  34. struct ogg_stream *os = ogg->streams + idx;
  35. AVStream *st = s->streams[idx];
  36. GetByteContext p;
  37. uint64_t time_unit;
  38. uint64_t spu;
  39. uint32_t size;
  40. bytestream2_init(&p, os->buf + os->pstart, os->psize);
  41. if (!(bytestream2_peek_byte(&p) & 1))
  42. return 0;
  43. if (bytestream2_peek_byte(&p) == 1) {
  44. bytestream2_skip(&p, 1);
  45. if (bytestream2_peek_byte(&p) == 'v'){
  46. int tag;
  47. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  48. bytestream2_skip(&p, 8);
  49. tag = bytestream2_get_le32(&p);
  50. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag);
  51. st->codec->codec_tag = tag;
  52. } else if (bytestream2_peek_byte(&p) == 't') {
  53. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  54. st->codec->codec_id = AV_CODEC_ID_TEXT;
  55. bytestream2_skip(&p, 12);
  56. } else {
  57. uint8_t acid[5] = { 0 };
  58. int cid;
  59. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  60. bytestream2_skip(&p, 8);
  61. bytestream2_get_buffer(&p, acid, 4);
  62. acid[4] = 0;
  63. cid = strtol(acid, NULL, 16);
  64. st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags, cid);
  65. // our parser completely breaks AAC in Ogg
  66. if (st->codec->codec_id != AV_CODEC_ID_AAC)
  67. st->need_parsing = AVSTREAM_PARSE_FULL;
  68. }
  69. size = bytestream2_get_le32(&p);
  70. size = FFMIN(size, os->psize);
  71. time_unit = bytestream2_get_le64(&p);
  72. spu = bytestream2_get_le64(&p);
  73. if (!time_unit || !spu) {
  74. av_log(s, AV_LOG_ERROR, "Invalid timing values.\n");
  75. return AVERROR_INVALIDDATA;
  76. }
  77. bytestream2_skip(&p, 4); /* default_len */
  78. bytestream2_skip(&p, 8); /* buffersize + bits_per_sample */
  79. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
  80. st->codec->width = bytestream2_get_le32(&p);
  81. st->codec->height = bytestream2_get_le32(&p);
  82. avpriv_set_pts_info(st, 64, time_unit, spu * 10000000);
  83. } else {
  84. st->codec->channels = bytestream2_get_le16(&p);
  85. bytestream2_skip(&p, 2); /* block_align */
  86. st->codec->bit_rate = bytestream2_get_le32(&p) * 8;
  87. st->codec->sample_rate = spu * 10000000 / time_unit;
  88. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  89. if (size >= 56 && st->codec->codec_id == AV_CODEC_ID_AAC) {
  90. bytestream2_skip(&p, 4);
  91. size -= 4;
  92. }
  93. if (size > 52) {
  94. av_assert0(FF_INPUT_BUFFER_PADDING_SIZE <= 52);
  95. size -= 52;
  96. ff_alloc_extradata(st->codec, size);
  97. bytestream2_get_buffer(&p, st->codec->extradata, st->codec->extradata_size);
  98. }
  99. }
  100. } else if (bytestream2_peek_byte(&p) == 3) {
  101. bytestream2_skip(&p, 7);
  102. if (bytestream2_get_bytes_left(&p) > 1)
  103. ff_vorbis_comment(s, &st->metadata, p.buffer, bytestream2_get_bytes_left(&p) - 1, 1);
  104. }
  105. return 1;
  106. }
  107. static int
  108. ogm_dshow_header(AVFormatContext *s, int idx)
  109. {
  110. struct ogg *ogg = s->priv_data;
  111. struct ogg_stream *os = ogg->streams + idx;
  112. AVStream *st = s->streams[idx];
  113. uint8_t *p = os->buf + os->pstart;
  114. uint32_t t;
  115. if(!(*p & 1))
  116. return 0;
  117. if(*p != 1)
  118. return 1;
  119. if (os->psize < 100)
  120. return AVERROR_INVALIDDATA;
  121. t = AV_RL32(p + 96);
  122. if(t == 0x05589f80){
  123. if (os->psize < 184)
  124. return AVERROR_INVALIDDATA;
  125. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  126. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, AV_RL32(p + 68));
  127. avpriv_set_pts_info(st, 64, AV_RL64(p + 164), 10000000);
  128. st->codec->width = AV_RL32(p + 176);
  129. st->codec->height = AV_RL32(p + 180);
  130. } else if(t == 0x05589f81){
  131. if (os->psize < 136)
  132. return AVERROR_INVALIDDATA;
  133. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  134. st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags, AV_RL16(p + 124));
  135. st->codec->channels = AV_RL16(p + 126);
  136. st->codec->sample_rate = AV_RL32(p + 128);
  137. st->codec->bit_rate = AV_RL32(p + 132) * 8;
  138. }
  139. return 1;
  140. }
  141. static int
  142. ogm_packet(AVFormatContext *s, int idx)
  143. {
  144. struct ogg *ogg = s->priv_data;
  145. struct ogg_stream *os = ogg->streams + idx;
  146. uint8_t *p = os->buf + os->pstart;
  147. int lb;
  148. if(*p & 8)
  149. os->pflags |= AV_PKT_FLAG_KEY;
  150. lb = ((*p & 2) << 1) | ((*p >> 6) & 3);
  151. os->pstart += lb + 1;
  152. os->psize -= lb + 1;
  153. while (lb--)
  154. os->pduration += p[lb+1] << (lb*8);
  155. return 0;
  156. }
  157. const struct ogg_codec ff_ogm_video_codec = {
  158. .magic = "\001video",
  159. .magicsize = 6,
  160. .header = ogm_header,
  161. .packet = ogm_packet,
  162. .granule_is_start = 1,
  163. .nb_header = 2,
  164. };
  165. const struct ogg_codec ff_ogm_audio_codec = {
  166. .magic = "\001audio",
  167. .magicsize = 6,
  168. .header = ogm_header,
  169. .packet = ogm_packet,
  170. .granule_is_start = 1,
  171. .nb_header = 2,
  172. };
  173. const struct ogg_codec ff_ogm_text_codec = {
  174. .magic = "\001text",
  175. .magicsize = 5,
  176. .header = ogm_header,
  177. .packet = ogm_packet,
  178. .granule_is_start = 1,
  179. .nb_header = 2,
  180. };
  181. const struct ogg_codec ff_ogm_old_codec = {
  182. .magic = "\001Direct Show Samples embedded in Ogg",
  183. .magicsize = 35,
  184. .header = ogm_dshow_header,
  185. .packet = ogm_packet,
  186. .granule_is_start = 1,
  187. .nb_header = 1,
  188. };