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.

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