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.

259 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. ogg_stream_init(&context->os, 31415);
  29. for(n = 0 ; n < avfcontext->nb_streams ; n++) {
  30. AVCodecContext *codec = &avfcontext->streams[n]->codec;
  31. uint8_t *p= codec->extradata;
  32. av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
  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, AVPacket *pkt)
  46. {
  47. OggContext *context = avfcontext->priv_data ;
  48. AVCodecContext *avctx= &avfcontext->streams[pkt->stream_index]->codec;
  49. ogg_packet *op= &context->op;
  50. ogg_page og ;
  51. int64_t pts;
  52. pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
  53. // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
  54. /* flush header packets so audio starts on a new page */
  55. if(!context->header_handled) {
  56. while(ogg_stream_flush(&context->os, &og)) {
  57. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  58. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  59. put_flush_packet(&avfcontext->pb);
  60. }
  61. context->header_handled = 1 ;
  62. }
  63. op->packet = (uint8_t*) pkt->data;
  64. op->bytes = pkt->size;
  65. op->b_o_s = op->packetno == 0;
  66. op->granulepos= pts;
  67. /* correct the fields in the packet -- essential for streaming */
  68. ogg_stream_packetin(&context->os, op);
  69. while(ogg_stream_pageout(&context->os, &og)) {
  70. put_buffer(&avfcontext->pb, og.header, og.header_len);
  71. put_buffer(&avfcontext->pb, og.body, og.body_len);
  72. put_flush_packet(&avfcontext->pb);
  73. }
  74. op->packetno++;
  75. return 0;
  76. }
  77. static int ogg_write_trailer(AVFormatContext *avfcontext) {
  78. OggContext *context = avfcontext->priv_data ;
  79. ogg_page og ;
  80. while(ogg_stream_flush(&context->os, &og)) {
  81. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  82. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  83. put_flush_packet(&avfcontext->pb);
  84. }
  85. ogg_stream_clear(&context->os) ;
  86. return 0 ;
  87. }
  88. static AVOutputFormat ogg_oformat = {
  89. "ogg",
  90. "Ogg Vorbis",
  91. "audio/x-vorbis",
  92. "ogg",
  93. sizeof(OggContext),
  94. CODEC_ID_VORBIS,
  95. 0,
  96. ogg_write_header,
  97. ogg_write_packet,
  98. ogg_write_trailer,
  99. } ;
  100. #endif //CONFIG_ENCODERS
  101. #if 0
  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. ogg_sync_init(&context->oy) ;
  131. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  132. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  133. return AVERROR_IO ;
  134. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  135. ogg_sync_pageout(&context->oy, &og) ;
  136. ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
  137. ogg_stream_pagein(&context->os, &og) ;
  138. /* currently only one vorbis stream supported */
  139. ast = av_new_stream(avfcontext, 0) ;
  140. if(!ast)
  141. return AVERROR_NOMEM ;
  142. av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
  143. codec= &ast->codec;
  144. codec->codec_type = CODEC_TYPE_AUDIO;
  145. codec->codec_id = CODEC_ID_VORBIS;
  146. for(i=0; i<3; i++){
  147. if(next_packet(avfcontext, &op)){
  148. return -1;
  149. }
  150. if(op.bytes >= (1<<16) || op.bytes < 0)
  151. return -1;
  152. codec->extradata_size+= 2 + op.bytes;
  153. codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  154. p= codec->extradata + codec->extradata_size - 2 - op.bytes;
  155. *(p++)= op.bytes>>8;
  156. *(p++)= op.bytes&0xFF;
  157. memcpy(p, op.packet, op.bytes);
  158. }
  159. return 0 ;
  160. }
  161. static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
  162. ogg_packet op ;
  163. if(next_packet(avfcontext, &op))
  164. return AVERROR_IO ;
  165. if(av_new_packet(pkt, op.bytes) < 0)
  166. return AVERROR_IO ;
  167. pkt->stream_index = 0 ;
  168. memcpy(pkt->data, op.packet, op.bytes);
  169. if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1)
  170. pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate);
  171. // printf("%lld %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
  172. return op.bytes;
  173. }
  174. static int ogg_read_close(AVFormatContext *avfcontext) {
  175. OggContext *context = avfcontext->priv_data ;
  176. ogg_stream_clear(&context->os) ;
  177. ogg_sync_clear(&context->oy) ;
  178. av_freep(&avfcontext->streams[0]->codec.extradata);
  179. return 0 ;
  180. }
  181. static AVInputFormat ogg_iformat = {
  182. "ogg",
  183. "Ogg Vorbis",
  184. sizeof(OggContext),
  185. NULL,
  186. ogg_read_header,
  187. ogg_read_packet,
  188. ogg_read_close,
  189. .extensions = "ogg",
  190. } ;
  191. #endif
  192. int libogg_init(void) {
  193. #ifdef CONFIG_ENCODERS
  194. av_register_output_format(&ogg_oformat) ;
  195. #endif
  196. /* av_register_input_format(&ogg_iformat); */
  197. return 0 ;
  198. }