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.3KB

  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. if(!size){
  55. // av_log(avfcontext, AV_LOG_DEBUG, "zero packet\n");
  56. return 0;
  57. }
  58. // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
  59. /* flush header packets so audio starts on a new page */
  60. if(!context->header_handled) {
  61. while(ogg_stream_flush(&context->os, &og)) {
  62. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  63. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  64. put_flush_packet(&avfcontext->pb);
  65. }
  66. context->header_handled = 1 ;
  67. }
  68. op->packet = (uint8_t*) buf;
  69. op->bytes = size;
  70. op->b_o_s = op->packetno == 0;
  71. op->granulepos= pts;
  72. /* correct the fields in the packet -- essential for streaming */
  73. ogg_stream_packetin(&context->os, op);
  74. while(ogg_stream_pageout(&context->os, &og)) {
  75. put_buffer(&avfcontext->pb, og.header, og.header_len);
  76. put_buffer(&avfcontext->pb, og.body, og.body_len);
  77. put_flush_packet(&avfcontext->pb);
  78. }
  79. op->packetno++;
  80. return 0;
  81. }
  82. static int ogg_write_trailer(AVFormatContext *avfcontext) {
  83. OggContext *context = avfcontext->priv_data ;
  84. ogg_page og ;
  85. while(ogg_stream_flush(&context->os, &og)) {
  86. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  87. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  88. put_flush_packet(&avfcontext->pb);
  89. }
  90. ogg_stream_clear(&context->os) ;
  91. return 0 ;
  92. }
  93. static AVOutputFormat ogg_oformat = {
  94. "ogg",
  95. "Ogg Vorbis",
  96. "audio/x-vorbis",
  97. "ogg",
  98. sizeof(OggContext),
  99. CODEC_ID_VORBIS,
  100. 0,
  101. ogg_write_header,
  102. ogg_write_packet,
  103. ogg_write_trailer,
  104. } ;
  105. #endif //CONFIG_ENCODERS
  106. static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
  107. OggContext *context = avfcontext->priv_data ;
  108. ogg_page og ;
  109. char *buf ;
  110. while(ogg_stream_packetout(&context->os, op) != 1) {
  111. /* while no pages are available, read in more data to the sync */
  112. while(ogg_sync_pageout(&context->oy, &og) != 1) {
  113. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  114. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  115. return 1 ;
  116. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  117. }
  118. /* got a page. Feed it into the stream and get the packet */
  119. if(ogg_stream_pagein(&context->os, &og) != 0)
  120. return 1 ;
  121. }
  122. return 0 ;
  123. }
  124. static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
  125. {
  126. OggContext *context = avfcontext->priv_data;
  127. ogg_packet op ;
  128. char *buf ;
  129. ogg_page og ;
  130. AVStream *ast ;
  131. AVCodecContext *codec;
  132. uint8_t *p;
  133. int i;
  134. avfcontext->ctx_flags |= AVFMTCTX_NOHEADER;
  135. ogg_sync_init(&context->oy) ;
  136. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  137. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  138. return -EIO ;
  139. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  140. ogg_sync_pageout(&context->oy, &og) ;
  141. ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
  142. ogg_stream_pagein(&context->os, &og) ;
  143. /* currently only one vorbis stream supported */
  144. ast = av_new_stream(avfcontext, 0) ;
  145. if(!ast)
  146. return AVERROR_NOMEM ;
  147. codec= &ast->codec;
  148. codec->codec_type = CODEC_TYPE_AUDIO;
  149. codec->codec_id = CODEC_ID_VORBIS;
  150. for(i=0; i<3; i++){
  151. if(next_packet(avfcontext, &op)){
  152. return -1;
  153. }
  154. codec->extradata_size+= 2 + op.bytes;
  155. codec->extradata= av_realloc(codec->extradata, codec->extradata_size);
  156. p= codec->extradata + codec->extradata_size - 2 - op.bytes;
  157. *(p++)= op.bytes>>8;
  158. *(p++)= op.bytes&0xFF;
  159. memcpy(p, op.packet, op.bytes);
  160. }
  161. return 0 ;
  162. }
  163. static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
  164. ogg_packet op ;
  165. if(next_packet(avfcontext, &op))
  166. return -EIO ;
  167. if(av_new_packet(pkt, op.bytes) < 0)
  168. return -EIO ;
  169. pkt->stream_index = 0 ;
  170. memcpy(pkt->data, op.packet, op.bytes);
  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. }