oggparseogm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/intreadwrite.h"
  23. #include "libavcodec/get_bits.h"
  24. #include "libavcodec/bytestream.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "oggdec.h"
  28. #include "riff.h"
  29. static int
  30. ogm_header(AVFormatContext *s, int idx)
  31. {
  32. struct ogg *ogg = s->priv_data;
  33. struct ogg_stream *os = ogg->streams + idx;
  34. AVStream *st = s->streams[idx];
  35. GetByteContext p;
  36. uint64_t time_unit;
  37. uint64_t spu;
  38. bytestream2_init(&p, os->buf + os->pstart, os->psize);
  39. if (!(bytestream2_peek_byte(&p) & 1))
  40. return 0;
  41. if (bytestream2_peek_byte(&p) == 1) {
  42. bytestream2_skip(&p, 1);
  43. if (bytestream2_peek_byte(&p) == 'v'){
  44. int tag;
  45. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  46. bytestream2_skip(&p, 8);
  47. tag = bytestream2_get_le32(&p);
  48. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag);
  49. st->codec->codec_tag = tag;
  50. } else if (bytestream2_peek_byte(&p) == 't') {
  51. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  52. st->codec->codec_id = CODEC_ID_TEXT;
  53. bytestream2_skip(&p, 12);
  54. } else {
  55. uint8_t acid[5] = { 0 };
  56. int cid;
  57. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  58. bytestream2_skip(&p, 8);
  59. bytestream2_get_buffer(&p, acid, 4);
  60. acid[4] = 0;
  61. cid = strtol(acid, NULL, 16);
  62. st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags, cid);
  63. st->need_parsing = AVSTREAM_PARSE_FULL;
  64. }
  65. bytestream2_skip(&p, 4); /* useless size field */
  66. time_unit = bytestream2_get_le64(&p);
  67. spu = bytestream2_get_le64(&p);
  68. if (!time_unit || !spu) {
  69. av_log(s, AV_LOG_ERROR, "Invalid timing values.\n");
  70. return AVERROR_INVALIDDATA;
  71. }
  72. bytestream2_skip(&p, 4); /* default_len */
  73. bytestream2_skip(&p, 8); /* buffersize + bits_per_sample */
  74. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
  75. st->codec->width = bytestream2_get_le32(&p);
  76. st->codec->height = bytestream2_get_le32(&p);
  77. st->codec->time_base.den = spu * 10000000;
  78. st->codec->time_base.num = time_unit;
  79. avpriv_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
  80. } else {
  81. st->codec->channels = bytestream2_get_le16(&p);
  82. bytestream2_skip(&p, 2); /* block_align */
  83. st->codec->bit_rate = bytestream2_get_le32(&p) * 8;
  84. st->codec->sample_rate = spu * 10000000 / time_unit;
  85. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  86. }
  87. } else if (bytestream2_peek_byte(&p) == 3) {
  88. bytestream2_skip(&p, 7);
  89. if (bytestream2_get_bytes_left(&p) > 1)
  90. ff_vorbis_comment(s, &st->metadata, p.buffer, bytestream2_get_bytes_left(&p) - 1);
  91. }
  92. return 1;
  93. }
  94. static int
  95. ogm_dshow_header(AVFormatContext *s, int idx)
  96. {
  97. struct ogg *ogg = s->priv_data;
  98. struct ogg_stream *os = ogg->streams + idx;
  99. AVStream *st = s->streams[idx];
  100. uint8_t *p = os->buf + os->pstart;
  101. uint32_t t;
  102. if(!(*p & 1))
  103. return 0;
  104. if(*p != 1)
  105. return 1;
  106. t = AV_RL32(p + 96);
  107. if(t == 0x05589f80){
  108. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  109. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, AV_RL32(p + 68));
  110. st->codec->time_base.den = 10000000;
  111. st->codec->time_base.num = AV_RL64(p + 164);
  112. st->codec->width = AV_RL32(p + 176);
  113. st->codec->height = AV_RL32(p + 180);
  114. } else if(t == 0x05589f81){
  115. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  116. st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags, AV_RL16(p + 124));
  117. st->codec->channels = AV_RL16(p + 126);
  118. st->codec->sample_rate = AV_RL32(p + 128);
  119. st->codec->bit_rate = AV_RL32(p + 132) * 8;
  120. }
  121. return 1;
  122. }
  123. static int
  124. ogm_packet(AVFormatContext *s, int idx)
  125. {
  126. struct ogg *ogg = s->priv_data;
  127. struct ogg_stream *os = ogg->streams + idx;
  128. uint8_t *p = os->buf + os->pstart;
  129. int lb;
  130. if(*p & 8)
  131. os->pflags |= AV_PKT_FLAG_KEY;
  132. lb = ((*p & 2) << 1) | ((*p >> 6) & 3);
  133. os->pstart += lb + 1;
  134. os->psize -= lb + 1;
  135. while (lb--)
  136. os->pduration += p[lb+1] << (lb*8);
  137. return 0;
  138. }
  139. const struct ogg_codec ff_ogm_video_codec = {
  140. .magic = "\001video",
  141. .magicsize = 6,
  142. .header = ogm_header,
  143. .packet = ogm_packet,
  144. .granule_is_start = 1,
  145. };
  146. const struct ogg_codec ff_ogm_audio_codec = {
  147. .magic = "\001audio",
  148. .magicsize = 6,
  149. .header = ogm_header,
  150. .packet = ogm_packet,
  151. .granule_is_start = 1,
  152. };
  153. const struct ogg_codec ff_ogm_text_codec = {
  154. .magic = "\001text",
  155. .magicsize = 5,
  156. .header = ogm_header,
  157. .packet = ogm_packet,
  158. .granule_is_start = 1,
  159. };
  160. const struct ogg_codec ff_ogm_old_codec = {
  161. .magic = "\001Direct Show Samples embedded in Ogg",
  162. .magicsize = 35,
  163. .header = ogm_dshow_header,
  164. .packet = ogm_packet,
  165. .granule_is_start = 1,
  166. };