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
7.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 <ogg/ogg.h>
  10. #include "avformat.h"
  11. #undef NDEBUG
  12. #include <assert.h>
  13. #define DECODER_BUFFER_SIZE 4096
  14. typedef struct OggContext {
  15. /* output */
  16. ogg_stream_state os ;
  17. int header_handled ;
  18. ogg_packet op;
  19. /* input */
  20. ogg_sync_state oy ;
  21. } OggContext ;
  22. #ifdef CONFIG_MUXERS
  23. static int ogg_write_header(AVFormatContext *avfcontext)
  24. {
  25. OggContext *context = avfcontext->priv_data;
  26. ogg_packet *op= &context->op;
  27. int n;
  28. ogg_stream_init(&context->os, 31415);
  29. for(n = 0 ; n < avfcontext->nb_streams ; n++) {
  30. AVCodecContext *codec = avfcontext->streams[n]->codec;
  31. uint8_t *headers = codec->extradata;
  32. int headers_len = codec->extradata_size;
  33. uint8_t *header_start[3];
  34. int header_len[3];
  35. int i, j;
  36. av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
  37. for(j=1,i=0;i<2;++i, ++j) {
  38. header_len[i]=0;
  39. while(j<headers_len && headers[j]==0xff) {
  40. header_len[i]+=0xff;
  41. ++j;
  42. }
  43. header_len[i]+=headers[j];
  44. }
  45. header_len[2]=headers_len-header_len[0]-header_len[1]-j;
  46. headers+=j;
  47. header_start[0] = headers;
  48. header_start[1] = header_start[0] + header_len[0];
  49. header_start[2] = header_start[1] + header_len[1];
  50. for(i=0; i < 3; ++i){
  51. op->bytes = header_len[i];
  52. op->packet= header_start[i];
  53. op->b_o_s= op->packetno==0;
  54. ogg_stream_packetin(&context->os, op);
  55. op->packetno++; //FIXME multiple streams
  56. }
  57. context->header_handled = 0 ;
  58. }
  59. return 0 ;
  60. }
  61. static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
  62. {
  63. OggContext *context = avfcontext->priv_data ;
  64. AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec;
  65. ogg_packet *op= &context->op;
  66. ogg_page og ;
  67. int64_t pts;
  68. pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
  69. // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
  70. /* flush header packets so audio starts on a new page */
  71. if(!context->header_handled) {
  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_handled = 1 ;
  78. }
  79. op->packet = (uint8_t*) pkt->data;
  80. op->bytes = pkt->size;
  81. op->b_o_s = op->packetno == 0;
  82. op->granulepos= pts;
  83. /* correct the fields in the packet -- essential for streaming */
  84. ogg_stream_packetin(&context->os, op);
  85. while(ogg_stream_pageout(&context->os, &og)) {
  86. put_buffer(&avfcontext->pb, og.header, og.header_len);
  87. put_buffer(&avfcontext->pb, og.body, og.body_len);
  88. put_flush_packet(&avfcontext->pb);
  89. }
  90. op->packetno++;
  91. return 0;
  92. }
  93. static int ogg_write_trailer(AVFormatContext *avfcontext) {
  94. OggContext *context = avfcontext->priv_data ;
  95. ogg_page og ;
  96. while(ogg_stream_flush(&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. ogg_stream_clear(&context->os) ;
  102. return 0 ;
  103. }
  104. AVOutputFormat ogg_muxer = {
  105. "ogg",
  106. "Ogg Vorbis",
  107. "audio/x-vorbis",
  108. "ogg",
  109. sizeof(OggContext),
  110. CODEC_ID_VORBIS,
  111. 0,
  112. ogg_write_header,
  113. ogg_write_packet,
  114. ogg_write_trailer,
  115. } ;
  116. #endif //CONFIG_MUXERS
  117. #if 0
  118. static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
  119. OggContext *context = avfcontext->priv_data ;
  120. ogg_page og ;
  121. char *buf ;
  122. while(ogg_stream_packetout(&context->os, op) != 1) {
  123. /* while no pages are available, read in more data to the sync */
  124. while(ogg_sync_pageout(&context->oy, &og) != 1) {
  125. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  126. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  127. return 1 ;
  128. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  129. }
  130. /* got a page. Feed it into the stream and get the packet */
  131. if(ogg_stream_pagein(&context->os, &og) != 0)
  132. return 1 ;
  133. }
  134. return 0 ;
  135. }
  136. static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
  137. {
  138. OggContext *context = avfcontext->priv_data;
  139. ogg_packet op ;
  140. char *buf ;
  141. ogg_page og ;
  142. AVStream *ast ;
  143. AVCodecContext *codec;
  144. uint8_t *p;
  145. int i;
  146. ogg_sync_init(&context->oy) ;
  147. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  148. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  149. return AVERROR_IO ;
  150. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  151. ogg_sync_pageout(&context->oy, &og) ;
  152. ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
  153. ogg_stream_pagein(&context->os, &og) ;
  154. /* currently only one vorbis stream supported */
  155. ast = av_new_stream(avfcontext, 0) ;
  156. if(!ast)
  157. return AVERROR_NOMEM ;
  158. av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
  159. codec= &ast->codec;
  160. codec->codec_type = CODEC_TYPE_AUDIO;
  161. codec->codec_id = CODEC_ID_VORBIS;
  162. for(i=0; i<3; i++){
  163. if(next_packet(avfcontext, &op)){
  164. return -1;
  165. }
  166. if(op.bytes >= (1<<16) || op.bytes < 0)
  167. return -1;
  168. codec->extradata_size+= 2 + op.bytes;
  169. codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  170. memset(codec->extradata + codec->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  171. p= codec->extradata + codec->extradata_size - 2 - op.bytes;
  172. *(p++)= op.bytes>>8;
  173. *(p++)= op.bytes&0xFF;
  174. memcpy(p, op.packet, op.bytes);
  175. }
  176. return 0 ;
  177. }
  178. static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
  179. ogg_packet op ;
  180. if(next_packet(avfcontext, &op))
  181. return AVERROR_IO ;
  182. if(av_new_packet(pkt, op.bytes) < 0)
  183. return AVERROR_IO ;
  184. pkt->stream_index = 0 ;
  185. memcpy(pkt->data, op.packet, op.bytes);
  186. if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1)
  187. pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate);
  188. // printf("%lld %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
  189. return op.bytes;
  190. }
  191. static int ogg_read_close(AVFormatContext *avfcontext) {
  192. OggContext *context = avfcontext->priv_data ;
  193. ogg_stream_clear(&context->os) ;
  194. ogg_sync_clear(&context->oy) ;
  195. return 0 ;
  196. }
  197. static AVInputFormat ogg_iformat = {
  198. "ogg",
  199. "Ogg Vorbis",
  200. sizeof(OggContext),
  201. NULL,
  202. ogg_read_header,
  203. ogg_read_packet,
  204. ogg_read_close,
  205. .extensions = "ogg",
  206. } ;
  207. #endif