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.

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