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.

268 lines
6.4KB

  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 <time.h>
  10. #include <ogg/ogg.h>
  11. #include <vorbis/vorbisenc.h>
  12. #include "avformat.h"
  13. #include "oggvorbis.h"
  14. #define DECODER_BUFFER_SIZE 4096
  15. typedef struct OggContext {
  16. /* output */
  17. ogg_stream_state os ;
  18. int header_handled ;
  19. ogg_int64_t base_packet_no ;
  20. ogg_int64_t base_granule_pos ;
  21. /* input */
  22. ogg_sync_state oy ;
  23. } OggContext ;
  24. #ifdef CONFIG_ENCODERS
  25. static int ogg_write_header(AVFormatContext *avfcontext)
  26. {
  27. OggContext *context = avfcontext->priv_data;
  28. AVCodecContext *avccontext ;
  29. vorbis_info vi ;
  30. vorbis_dsp_state vd ;
  31. vorbis_comment vc ;
  32. vorbis_block vb ;
  33. ogg_packet header, header_comm, header_code ;
  34. int n ;
  35. srand(time(NULL));
  36. ogg_stream_init(&context->os, rand());
  37. for(n = 0 ; n < avfcontext->nb_streams ; n++) {
  38. avccontext = &avfcontext->streams[n]->codec ;
  39. /* begin vorbis specific code */
  40. vorbis_info_init(&vi) ;
  41. /* code copied from libavcodec/oggvorbis.c */
  42. if(oggvorbis_init_encoder(&vi, avccontext) < 0) {
  43. fprintf(stderr, "ogg_write_header: init_encoder failed") ;
  44. return -1 ;
  45. }
  46. vorbis_analysis_init(&vd, &vi) ;
  47. vorbis_block_init(&vd, &vb) ;
  48. vorbis_comment_init(&vc) ;
  49. vorbis_comment_add_tag(&vc, "encoder", LIBAVFORMAT_IDENT) ;
  50. if(*avfcontext->title)
  51. vorbis_comment_add_tag(&vc, "title", avfcontext->title) ;
  52. vorbis_analysis_headerout(&vd, &vc, &header,
  53. &header_comm, &header_code) ;
  54. ogg_stream_packetin(&context->os, &header) ;
  55. ogg_stream_packetin(&context->os, &header_comm) ;
  56. ogg_stream_packetin(&context->os, &header_code) ;
  57. vorbis_block_clear(&vb) ;
  58. vorbis_dsp_clear(&vd) ;
  59. vorbis_info_clear(&vi) ;
  60. vorbis_comment_clear(&vc) ;
  61. /* end of vorbis specific code */
  62. context->header_handled = 0 ;
  63. context->base_packet_no = 0 ;
  64. }
  65. return 0 ;
  66. }
  67. static int ogg_write_packet(AVFormatContext *avfcontext,
  68. int stream_index,
  69. const uint8_t *buf, int size, int64_t force_pts)
  70. {
  71. OggContext *context = avfcontext->priv_data ;
  72. ogg_packet *op ;
  73. ogg_page og ;
  74. int l = 0 ;
  75. /* flush header packets so audio starts on a new page */
  76. if(!context->header_handled) {
  77. while(ogg_stream_flush(&context->os, &og)) {
  78. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  79. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  80. put_flush_packet(&avfcontext->pb);
  81. }
  82. context->header_handled = 1 ;
  83. }
  84. while(l < size) {
  85. op = (ogg_packet*)(buf + l) ;
  86. op->packet = (uint8_t*) buf + l + sizeof( ogg_packet) ; /* fix data pointer */
  87. if(!context->base_packet_no) { /* this is the first packet */
  88. context->base_packet_no = op->packetno ;
  89. context->base_granule_pos = op->granulepos ;
  90. }
  91. /* correct the fields in the packet -- essential for streaming */
  92. op->packetno -= context->base_packet_no ;
  93. op->granulepos -= context->base_granule_pos ;
  94. ogg_stream_packetin(&context->os, op) ;
  95. l += sizeof(ogg_packet) + op->bytes ;
  96. while(ogg_stream_pageout(&context->os, &og)) {
  97. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  98. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  99. put_flush_packet(&avfcontext->pb);
  100. }
  101. }
  102. return 0;
  103. }
  104. static int ogg_write_trailer(AVFormatContext *avfcontext) {
  105. OggContext *context = avfcontext->priv_data ;
  106. ogg_page og ;
  107. while(ogg_stream_flush(&context->os, &og)) {
  108. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  109. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  110. put_flush_packet(&avfcontext->pb);
  111. }
  112. ogg_stream_clear(&context->os) ;
  113. return 0 ;
  114. }
  115. static AVOutputFormat ogg_oformat = {
  116. "ogg",
  117. "Ogg Vorbis",
  118. "audio/x-vorbis",
  119. "ogg",
  120. sizeof(OggContext),
  121. CODEC_ID_VORBIS,
  122. 0,
  123. ogg_write_header,
  124. ogg_write_packet,
  125. ogg_write_trailer,
  126. } ;
  127. #endif //CONFIG_ENCODERS
  128. static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
  129. OggContext *context = avfcontext->priv_data ;
  130. ogg_page og ;
  131. char *buf ;
  132. while(ogg_stream_packetout(&context->os, op) != 1) {
  133. /* while no pages are available, read in more data to the sync */
  134. while(ogg_sync_pageout(&context->oy, &og) != 1) {
  135. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  136. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  137. return 1 ;
  138. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  139. }
  140. /* got a page. Feed it into the stream and get the packet */
  141. if(ogg_stream_pagein(&context->os, &og) != 0)
  142. return 1 ;
  143. }
  144. return 0 ;
  145. }
  146. static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
  147. {
  148. OggContext *context = avfcontext->priv_data;
  149. char *buf ;
  150. ogg_page og ;
  151. AVStream *ast ;
  152. ogg_sync_init(&context->oy) ;
  153. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  154. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  155. return -EIO ;
  156. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  157. ogg_sync_pageout(&context->oy, &og) ;
  158. ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
  159. ogg_stream_pagein(&context->os, &og) ;
  160. /* currently only one vorbis stream supported */
  161. ast = av_new_stream(avfcontext, 0) ;
  162. if(!ast)
  163. return AVERROR_NOMEM ;
  164. ast->codec.codec_type = CODEC_TYPE_AUDIO ;
  165. ast->codec.codec_id = CODEC_ID_VORBIS ;
  166. return 0 ;
  167. }
  168. static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
  169. ogg_packet op ;
  170. if(next_packet(avfcontext, &op))
  171. return -EIO ;
  172. if(av_new_packet(pkt, sizeof(ogg_packet) + op.bytes) < 0)
  173. return -EIO ;
  174. pkt->stream_index = 0 ;
  175. memcpy(pkt->data, &op, sizeof(ogg_packet)) ;
  176. memcpy(pkt->data + sizeof(ogg_packet), op.packet, op.bytes) ;
  177. return sizeof(ogg_packet) + op.bytes ;
  178. }
  179. static int ogg_read_close(AVFormatContext *avfcontext) {
  180. OggContext *context = avfcontext->priv_data ;
  181. ogg_stream_clear(&context->os) ;
  182. ogg_sync_clear(&context->oy) ;
  183. return 0 ;
  184. }
  185. static AVInputFormat ogg_iformat = {
  186. "ogg",
  187. "Ogg Vorbis",
  188. sizeof(OggContext),
  189. NULL,
  190. ogg_read_header,
  191. ogg_read_packet,
  192. ogg_read_close,
  193. .extensions = "ogg",
  194. } ;
  195. int ogg_init(void) {
  196. #ifdef CONFIG_ENCODERS
  197. av_register_output_format(&ogg_oformat) ;
  198. #endif
  199. av_register_input_format(&ogg_iformat);
  200. return 0 ;
  201. }