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.

261 lines
6.2KB

  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_comment_clear(&vc) ;
  57. /* end of vorbis specific code */
  58. context->header_handled = 0 ;
  59. context->base_packet_no = 0 ;
  60. }
  61. return 0 ;
  62. }
  63. static int ogg_write_packet(AVFormatContext *avfcontext,
  64. int stream_index,
  65. unsigned char *buf, int size, int force_pts)
  66. {
  67. OggContext *context = avfcontext->priv_data ;
  68. ogg_packet *op ;
  69. ogg_page og ;
  70. int l = 0 ;
  71. /* flush header packets so audio starts on a new page */
  72. if(!context->header_handled) {
  73. while(ogg_stream_flush(&context->os, &og)) {
  74. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  75. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  76. put_flush_packet(&avfcontext->pb);
  77. }
  78. context->header_handled = 1 ;
  79. }
  80. while(l < size) {
  81. op = (ogg_packet*)(buf + l) ;
  82. op->packet = buf + l + sizeof(ogg_packet) ; /* fix data pointer */
  83. if(!context->base_packet_no) { /* this is the first packet */
  84. context->base_packet_no = op->packetno ;
  85. context->base_granule_pos = op->granulepos ;
  86. }
  87. /* correct the fields in the packet -- essential for streaming */
  88. op->packetno -= context->base_packet_no ;
  89. op->granulepos -= context->base_granule_pos ;
  90. ogg_stream_packetin(&context->os, op) ;
  91. l += sizeof(ogg_packet) + op->bytes ;
  92. while(ogg_stream_pageout(&context->os, &og)) {
  93. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  94. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  95. put_flush_packet(&avfcontext->pb);
  96. }
  97. }
  98. return 0;
  99. }
  100. static int ogg_write_trailer(AVFormatContext *avfcontext) {
  101. OggContext *context = avfcontext->priv_data ;
  102. ogg_page og ;
  103. while(ogg_stream_flush(&context->os, &og)) {
  104. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  105. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  106. put_flush_packet(&avfcontext->pb);
  107. }
  108. ogg_stream_clear(&context->os) ;
  109. return 0 ;
  110. }
  111. static AVOutputFormat ogg_oformat = {
  112. "ogg",
  113. "Ogg Vorbis",
  114. "audio/x-vorbis",
  115. "ogg",
  116. sizeof(OggContext),
  117. CODEC_ID_VORBIS,
  118. 0,
  119. ogg_write_header,
  120. ogg_write_packet,
  121. ogg_write_trailer,
  122. } ;
  123. static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
  124. OggContext *context = avfcontext->priv_data ;
  125. ogg_page og ;
  126. char *buf ;
  127. while(ogg_stream_packetout(&context->os, op) != 1) {
  128. /* while no pages are available, read in more data to the sync */
  129. while(ogg_sync_pageout(&context->oy, &og) != 1) {
  130. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  131. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  132. return 1 ;
  133. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  134. }
  135. /* got a page. Feed it into the stream and get the packet */
  136. if(ogg_stream_pagein(&context->os, &og) != 0)
  137. return 1 ;
  138. }
  139. return 0 ;
  140. }
  141. static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
  142. {
  143. OggContext *context = avfcontext->priv_data;
  144. char *buf ;
  145. ogg_page og ;
  146. AVStream *ast ;
  147. ogg_sync_init(&context->oy) ;
  148. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  149. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  150. return -EIO ;
  151. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  152. ogg_sync_pageout(&context->oy, &og) ;
  153. ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
  154. ogg_stream_pagein(&context->os, &og) ;
  155. /* currently only one vorbis stream supported */
  156. ast = av_new_stream(avfcontext, 0) ;
  157. if(!ast)
  158. return AVERROR_NOMEM ;
  159. ast->codec.codec_type = CODEC_TYPE_AUDIO ;
  160. ast->codec.codec_id = CODEC_ID_VORBIS ;
  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, sizeof(ogg_packet) + op.bytes) < 0)
  168. return -EIO ;
  169. pkt->stream_index = 0 ;
  170. memcpy(pkt->data, &op, sizeof(ogg_packet)) ;
  171. memcpy(pkt->data + sizeof(ogg_packet), op.packet, op.bytes) ;
  172. return sizeof(ogg_packet) + 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. 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. av_register_output_format(&ogg_oformat) ;
  192. av_register_input_format(&ogg_iformat);
  193. return 0 ;
  194. }