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.

169 lines
4.0KB

  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. typedef struct OggContext {
  15. ogg_stream_state os ;
  16. int header_written ;
  17. ogg_int64_t base_packet_no ;
  18. ogg_int64_t base_granule_pos ;
  19. } OggContext ;
  20. static int ogg_write_header(AVFormatContext *avfcontext) {
  21. OggContext *context ;
  22. AVCodecContext *avccontext ;
  23. vorbis_info vi ;
  24. vorbis_dsp_state vd ;
  25. vorbis_comment vc ;
  26. vorbis_block vb ;
  27. ogg_packet header, header_comm, header_code ;
  28. int n ;
  29. fprintf(stderr, "ogg_write_header\n") ;
  30. if(!(context = malloc(sizeof(OggContext))))
  31. return -1 ;
  32. avfcontext->priv_data = context ;
  33. srand(time(NULL));
  34. ogg_stream_init(&context->os, rand());
  35. for(n = 0 ; n < avfcontext->nb_streams ; n++) {
  36. avccontext = &avfcontext->streams[n]->codec ;
  37. /* begin vorbis specific code */
  38. vorbis_info_init(&vi) ;
  39. /* code copied from libavcodec/oggvorbis.c */
  40. if(oggvorbis_init_encoder(&vi, avccontext) < 0) {
  41. fprintf(stderr, "ogg_write_header: init_encoder failed") ;
  42. return -1 ;
  43. }
  44. vorbis_analysis_init(&vd, &vi) ;
  45. vorbis_block_init(&vd, &vb) ;
  46. vorbis_comment_init(&vc) ;
  47. vorbis_comment_add_tag(&vc, "encoder", "ffmpeg") ;
  48. if(*avfcontext->title)
  49. vorbis_comment_add_tag(&vc, "title", avfcontext->title) ;
  50. vorbis_analysis_headerout(&vd, &vc, &header,
  51. &header_comm, &header_code) ;
  52. ogg_stream_packetin(&context->os, &header) ;
  53. ogg_stream_packetin(&context->os, &header_comm) ;
  54. ogg_stream_packetin(&context->os, &header_code) ;
  55. vorbis_comment_clear(&vc) ;
  56. /* end of vorbis specific code */
  57. context->header_written = 0 ;
  58. context->base_packet_no = 0 ;
  59. }
  60. return 0 ;
  61. }
  62. static int ogg_write_packet(AVFormatContext *avfcontext,
  63. int stream_index,
  64. unsigned char *buf, int size, int force_pts)
  65. {
  66. OggContext *context = avfcontext->priv_data ;
  67. ogg_packet *op ;
  68. ogg_page og ;
  69. int l = 0 ;
  70. /* flush header packets so audio starts on a new page */
  71. if(!context->header_written) {
  72. while(ogg_stream_flush(&context->os, &og)) {
  73. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  74. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  75. put_flush_packet(&avfcontext->pb);
  76. }
  77. context->header_written = 1 ;
  78. }
  79. while(l < size) {
  80. op = (ogg_packet*)(buf + l) ;
  81. op->packet = buf + l + sizeof(ogg_packet) ; /* fix data pointer */
  82. if(!context->base_packet_no) { /* this is the first packet */
  83. context->base_packet_no = op->packetno ;
  84. context->base_granule_pos = op->granulepos ;
  85. }
  86. /* correct the fields in the packet -- essential for streaming */
  87. op->packetno -= context->base_packet_no ;
  88. op->granulepos -= context->base_granule_pos ;
  89. ogg_stream_packetin(&context->os, op) ;
  90. l += sizeof(ogg_packet) + op->bytes ;
  91. while(ogg_stream_pageout(&context->os, &og)) {
  92. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  93. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  94. put_flush_packet(&avfcontext->pb);
  95. }
  96. }
  97. return 0;
  98. }
  99. static int ogg_write_trailer(AVFormatContext *avfcontext) {
  100. OggContext *context = avfcontext->priv_data ;
  101. ogg_page og ;
  102. fprintf(stderr, "ogg_write_trailer\n") ;
  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. 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. int ogg_init(void) {
  124. av_register_output_format(&ogg_oformat) ;
  125. return 0 ;
  126. }