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.

260 lines
6.6KB

  1. /*
  2. * Ogg bitstream support
  3. * Mark Hills <mark@pogo.org.uk>
  4. *
  5. * Uses libogg, but requires libvorbisenc to construct correct headers
  6. * when containing Vorbis stream -- currently the only format supported
  7. */
  8. #include <stdio.h>
  9. #include <ogg/ogg.h>
  10. #include "avformat.h"
  11. #undef NDEBUG
  12. #include <assert.h>
  13. #define DECODER_BUFFER_SIZE 4096
  14. typedef struct OggContext {
  15. /* output */
  16. ogg_stream_state os ;
  17. int header_handled ;
  18. ogg_packet op;
  19. /* input */
  20. ogg_sync_state oy ;
  21. } OggContext ;
  22. #ifdef CONFIG_ENCODERS
  23. static int ogg_write_header(AVFormatContext *avfcontext)
  24. {
  25. OggContext *context = avfcontext->priv_data;
  26. ogg_packet *op= &context->op;
  27. int n, i;
  28. av_set_pts_info(avfcontext, 60, 1, AV_TIME_BASE);
  29. ogg_stream_init(&context->os, 31415);
  30. for(n = 0 ; n < avfcontext->nb_streams ; n++) {
  31. AVCodecContext *codec = &avfcontext->streams[n]->codec;
  32. uint8_t *p= codec->extradata;
  33. for(i=0; i < codec->extradata_size; i+= op->bytes){
  34. op->bytes = p[i++]<<8;
  35. op->bytes+= p[i++];
  36. op->packet= &p[i];
  37. op->b_o_s= op->packetno==0;
  38. ogg_stream_packetin(&context->os, op);
  39. op->packetno++; //FIXME multiple streams
  40. }
  41. context->header_handled = 0 ;
  42. }
  43. return 0 ;
  44. }
  45. static int ogg_write_packet(AVFormatContext *avfcontext,
  46. int stream_index,
  47. const uint8_t *buf, int size, int64_t pts)
  48. {
  49. OggContext *context = avfcontext->priv_data ;
  50. AVCodecContext *avctx= &avfcontext->streams[stream_index]->codec;
  51. ogg_packet *op= &context->op;
  52. ogg_page og ;
  53. pts= av_rescale(pts, avctx->sample_rate, AV_TIME_BASE);
  54. // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
  55. /* flush header packets so audio starts on a new page */
  56. if(!context->header_handled) {
  57. while(ogg_stream_flush(&context->os, &og)) {
  58. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  59. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  60. put_flush_packet(&avfcontext->pb);
  61. }
  62. context->header_handled = 1 ;
  63. }
  64. op->packet = (uint8_t*) buf;
  65. op->bytes = size;
  66. op->b_o_s = op->packetno == 0;
  67. op->granulepos= pts;
  68. /* correct the fields in the packet -- essential for streaming */
  69. ogg_stream_packetin(&context->os, op);
  70. while(ogg_stream_pageout(&context->os, &og)) {
  71. put_buffer(&avfcontext->pb, og.header, og.header_len);
  72. put_buffer(&avfcontext->pb, og.body, og.body_len);
  73. put_flush_packet(&avfcontext->pb);
  74. }
  75. op->packetno++;
  76. return 0;
  77. }
  78. static int ogg_write_trailer(AVFormatContext *avfcontext) {
  79. OggContext *context = avfcontext->priv_data ;
  80. ogg_page og ;
  81. while(ogg_stream_flush(&context->os, &og)) {
  82. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  83. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  84. put_flush_packet(&avfcontext->pb);
  85. }
  86. ogg_stream_clear(&context->os) ;
  87. return 0 ;
  88. }
  89. static AVOutputFormat ogg_oformat = {
  90. "ogg",
  91. "Ogg Vorbis",
  92. "audio/x-vorbis",
  93. "ogg",
  94. sizeof(OggContext),
  95. CODEC_ID_VORBIS,
  96. 0,
  97. ogg_write_header,
  98. ogg_write_packet,
  99. ogg_write_trailer,
  100. } ;
  101. #endif //CONFIG_ENCODERS
  102. static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
  103. OggContext *context = avfcontext->priv_data ;
  104. ogg_page og ;
  105. char *buf ;
  106. while(ogg_stream_packetout(&context->os, op) != 1) {
  107. /* while no pages are available, read in more data to the sync */
  108. while(ogg_sync_pageout(&context->oy, &og) != 1) {
  109. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  110. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  111. return 1 ;
  112. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  113. }
  114. /* got a page. Feed it into the stream and get the packet */
  115. if(ogg_stream_pagein(&context->os, &og) != 0)
  116. return 1 ;
  117. }
  118. return 0 ;
  119. }
  120. static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
  121. {
  122. OggContext *context = avfcontext->priv_data;
  123. ogg_packet op ;
  124. char *buf ;
  125. ogg_page og ;
  126. AVStream *ast ;
  127. AVCodecContext *codec;
  128. uint8_t *p;
  129. int i;
  130. avfcontext->ctx_flags |= AVFMTCTX_NOHEADER;
  131. av_set_pts_info(avfcontext, 60, 1, AV_TIME_BASE);
  132. ogg_sync_init(&context->oy) ;
  133. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  134. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  135. return -EIO ;
  136. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  137. ogg_sync_pageout(&context->oy, &og) ;
  138. ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
  139. ogg_stream_pagein(&context->os, &og) ;
  140. /* currently only one vorbis stream supported */
  141. ast = av_new_stream(avfcontext, 0) ;
  142. if(!ast)
  143. return AVERROR_NOMEM ;
  144. codec= &ast->codec;
  145. codec->codec_type = CODEC_TYPE_AUDIO;
  146. codec->codec_id = CODEC_ID_VORBIS;
  147. for(i=0; i<3; i++){
  148. if(next_packet(avfcontext, &op)){
  149. return -1;
  150. }
  151. codec->extradata_size+= 2 + op.bytes;
  152. codec->extradata= av_realloc(codec->extradata, codec->extradata_size);
  153. p= codec->extradata + codec->extradata_size - 2 - op.bytes;
  154. *(p++)= op.bytes>>8;
  155. *(p++)= op.bytes&0xFF;
  156. memcpy(p, op.packet, op.bytes);
  157. }
  158. return 0 ;
  159. }
  160. static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
  161. ogg_packet op ;
  162. if(next_packet(avfcontext, &op))
  163. return -EIO ;
  164. if(av_new_packet(pkt, op.bytes) < 0)
  165. return -EIO ;
  166. pkt->stream_index = 0 ;
  167. memcpy(pkt->data, op.packet, op.bytes);
  168. if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1)
  169. pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate);
  170. // printf("%lld %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
  171. return op.bytes;
  172. }
  173. static int ogg_read_close(AVFormatContext *avfcontext) {
  174. OggContext *context = avfcontext->priv_data ;
  175. ogg_stream_clear(&context->os) ;
  176. ogg_sync_clear(&context->oy) ;
  177. av_freep(&avfcontext->streams[0]->codec.extradata);
  178. return 0 ;
  179. }
  180. static AVInputFormat ogg_iformat = {
  181. "ogg",
  182. "Ogg Vorbis",
  183. sizeof(OggContext),
  184. NULL,
  185. ogg_read_header,
  186. ogg_read_packet,
  187. ogg_read_close,
  188. .extensions = "ogg",
  189. } ;
  190. int ogg_init(void) {
  191. #ifdef CONFIG_ENCODERS
  192. av_register_output_format(&ogg_oformat) ;
  193. #endif
  194. av_register_input_format(&ogg_iformat);
  195. return 0 ;
  196. }