oggdec.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #ifndef AVFORMAT_OGGDEC_H
  22. #define AVFORMAT_OGGDEC_H
  23. #include "avformat.h"
  24. #include "metadata.h"
  25. struct ogg_codec {
  26. const int8_t *magic;
  27. uint8_t magicsize;
  28. const int8_t *name;
  29. /**
  30. * Attempt to process a packet as a header
  31. * @return 1 if the packet was a valid header,
  32. * 0 if the packet was not a header (was a data packet)
  33. * -1 if an error occurred or for unsupported stream
  34. */
  35. int (*header)(AVFormatContext *, int);
  36. int (*packet)(AVFormatContext *, int);
  37. /**
  38. * Translate a granule into a timestamp.
  39. * Will set dts if non-null and known.
  40. * @return pts
  41. */
  42. uint64_t (*gptopts)(AVFormatContext *, int, uint64_t, int64_t *dts);
  43. /**
  44. * 1 if granule is the start time of the associated packet.
  45. * 0 if granule is the end time of the associated packet.
  46. */
  47. int granule_is_start;
  48. /**
  49. * Number of expected headers
  50. */
  51. int nb_header;
  52. void (*cleanup)(AVFormatContext *s, int idx);
  53. };
  54. struct ogg_stream {
  55. uint8_t *buf;
  56. unsigned int bufsize;
  57. unsigned int bufpos;
  58. unsigned int pstart;
  59. unsigned int psize;
  60. unsigned int pflags;
  61. unsigned int pduration;
  62. uint32_t serial;
  63. uint64_t granule;
  64. uint64_t start_granule;
  65. int64_t lastpts;
  66. int64_t lastdts;
  67. int64_t sync_pos; ///< file offset of the first page needed to reconstruct the current packet
  68. int64_t page_pos; ///< file offset of the current page
  69. int flags;
  70. const struct ogg_codec *codec;
  71. int header;
  72. int nsegs, segp;
  73. uint8_t segments[255];
  74. int incomplete; ///< whether we're expecting a continuation in the next page
  75. int page_end; ///< current packet is the last one completed in the page
  76. int keyframe_seek;
  77. int got_start;
  78. int got_data; ///< 1 if the stream got some data (non-initial packets), 0 otherwise
  79. int nb_header; ///< set to the number of parsed headers
  80. int start_trimming; ///< set the number of packets to drop from the start
  81. int end_trimming; ///< set the number of packets to drop from the end
  82. uint8_t *new_metadata;
  83. unsigned int new_metadata_size;
  84. void *private;
  85. };
  86. struct ogg_state {
  87. uint64_t pos;
  88. int curidx;
  89. struct ogg_state *next;
  90. int nstreams;
  91. struct ogg_stream streams[1];
  92. };
  93. struct ogg {
  94. struct ogg_stream *streams;
  95. int nstreams;
  96. int headers;
  97. int curidx;
  98. int64_t page_pos; ///< file offset of the current page
  99. struct ogg_state *state;
  100. };
  101. #define OGG_FLAG_CONT 1
  102. #define OGG_FLAG_BOS 2
  103. #define OGG_FLAG_EOS 4
  104. #define OGG_NOGRANULE_VALUE (-1ull)
  105. extern const struct ogg_codec ff_celt_codec;
  106. extern const struct ogg_codec ff_dirac_codec;
  107. extern const struct ogg_codec ff_flac_codec;
  108. extern const struct ogg_codec ff_ogm_audio_codec;
  109. extern const struct ogg_codec ff_ogm_old_codec;
  110. extern const struct ogg_codec ff_ogm_text_codec;
  111. extern const struct ogg_codec ff_ogm_video_codec;
  112. extern const struct ogg_codec ff_old_dirac_codec;
  113. extern const struct ogg_codec ff_old_flac_codec;
  114. extern const struct ogg_codec ff_opus_codec;
  115. extern const struct ogg_codec ff_skeleton_codec;
  116. extern const struct ogg_codec ff_speex_codec;
  117. extern const struct ogg_codec ff_theora_codec;
  118. extern const struct ogg_codec ff_vorbis_codec;
  119. extern const struct ogg_codec ff_vp8_codec;
  120. int ff_vorbis_comment(AVFormatContext *ms, AVDictionary **m,
  121. const uint8_t *buf, int size, int parse_picture);
  122. int ff_vorbis_stream_comment(AVFormatContext *as, AVStream *st,
  123. const uint8_t *buf, int size);
  124. static inline int
  125. ogg_find_stream (struct ogg * ogg, int serial)
  126. {
  127. int i;
  128. for (i = 0; i < ogg->nstreams; i++)
  129. if (ogg->streams[i].serial == serial)
  130. return i;
  131. return -1;
  132. }
  133. static inline uint64_t
  134. ogg_gptopts (AVFormatContext * s, int i, uint64_t gp, int64_t *dts)
  135. {
  136. struct ogg *ogg = s->priv_data;
  137. struct ogg_stream *os = ogg->streams + i;
  138. uint64_t pts = AV_NOPTS_VALUE;
  139. if(os->codec && os->codec->gptopts){
  140. pts = os->codec->gptopts(s, i, gp, dts);
  141. } else {
  142. pts = gp;
  143. if (dts)
  144. *dts = pts;
  145. }
  146. if (pts > INT64_MAX && pts != AV_NOPTS_VALUE) {
  147. // The return type is unsigned, we thus cannot return negative pts
  148. av_log(s, AV_LOG_ERROR, "invalid pts %"PRId64"\n", pts);
  149. pts = AV_NOPTS_VALUE;
  150. }
  151. return pts;
  152. }
  153. #endif /* AVFORMAT_OGGDEC_H */