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.

284 lines
7.7KB

  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. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <stdio.h>
  25. #include <ogg/ogg.h>
  26. #include "avformat.h"
  27. #undef NDEBUG
  28. #include <assert.h>
  29. #define DECODER_BUFFER_SIZE 4096
  30. typedef struct OggContext {
  31. /* output */
  32. ogg_stream_state os ;
  33. int header_handled ;
  34. ogg_packet op;
  35. /* input */
  36. ogg_sync_state oy ;
  37. } OggContext ;
  38. #ifdef CONFIG_MUXERS
  39. static int ogg_write_header(AVFormatContext *avfcontext)
  40. {
  41. OggContext *context = avfcontext->priv_data;
  42. ogg_packet *op= &context->op;
  43. int n;
  44. ogg_stream_init(&context->os, 31415);
  45. for(n = 0 ; n < avfcontext->nb_streams ; n++) {
  46. AVCodecContext *codec = avfcontext->streams[n]->codec;
  47. uint8_t *headers = codec->extradata;
  48. int headers_len = codec->extradata_size;
  49. uint8_t *header_start[3];
  50. int header_len[3];
  51. int i, j;
  52. av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
  53. for(j=1,i=0;i<2;++i, ++j) {
  54. header_len[i]=0;
  55. while(j<headers_len && headers[j]==0xff) {
  56. header_len[i]+=0xff;
  57. ++j;
  58. }
  59. header_len[i]+=headers[j];
  60. }
  61. header_len[2]=headers_len-header_len[0]-header_len[1]-j;
  62. headers+=j;
  63. header_start[0] = headers;
  64. header_start[1] = header_start[0] + header_len[0];
  65. header_start[2] = header_start[1] + header_len[1];
  66. for(i=0; i < 3; ++i){
  67. op->bytes = header_len[i];
  68. op->packet= header_start[i];
  69. op->b_o_s= op->packetno==0;
  70. ogg_stream_packetin(&context->os, op);
  71. op->packetno++; //FIXME multiple streams
  72. }
  73. context->header_handled = 0 ;
  74. }
  75. return 0 ;
  76. }
  77. static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
  78. {
  79. OggContext *context = avfcontext->priv_data ;
  80. AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec;
  81. ogg_packet *op= &context->op;
  82. ogg_page og ;
  83. int64_t pts;
  84. pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
  85. // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
  86. /* flush header packets so audio starts on a new page */
  87. if(!context->header_handled) {
  88. while(ogg_stream_flush(&context->os, &og)) {
  89. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  90. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  91. put_flush_packet(&avfcontext->pb);
  92. }
  93. context->header_handled = 1 ;
  94. }
  95. op->packet = (uint8_t*) pkt->data;
  96. op->bytes = pkt->size;
  97. op->b_o_s = op->packetno == 0;
  98. op->granulepos= pts;
  99. /* correct the fields in the packet -- essential for streaming */
  100. ogg_stream_packetin(&context->os, op);
  101. while(ogg_stream_pageout(&context->os, &og)) {
  102. put_buffer(&avfcontext->pb, og.header, og.header_len);
  103. put_buffer(&avfcontext->pb, og.body, og.body_len);
  104. put_flush_packet(&avfcontext->pb);
  105. }
  106. op->packetno++;
  107. return 0;
  108. }
  109. static int ogg_write_trailer(AVFormatContext *avfcontext) {
  110. OggContext *context = avfcontext->priv_data ;
  111. ogg_page og ;
  112. while(ogg_stream_flush(&context->os, &og)) {
  113. put_buffer(&avfcontext->pb, og.header, og.header_len) ;
  114. put_buffer(&avfcontext->pb, og.body, og.body_len) ;
  115. put_flush_packet(&avfcontext->pb);
  116. }
  117. ogg_stream_clear(&context->os) ;
  118. return 0 ;
  119. }
  120. AVOutputFormat ogg_muxer = {
  121. "ogg",
  122. "Ogg format",
  123. "application/ogg",
  124. "ogg",
  125. sizeof(OggContext),
  126. CODEC_ID_VORBIS,
  127. 0,
  128. ogg_write_header,
  129. ogg_write_packet,
  130. ogg_write_trailer,
  131. } ;
  132. #endif //CONFIG_MUXERS
  133. #if 0
  134. static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
  135. OggContext *context = avfcontext->priv_data ;
  136. ogg_page og ;
  137. char *buf ;
  138. while(ogg_stream_packetout(&context->os, op) != 1) {
  139. /* while no pages are available, read in more data to the sync */
  140. while(ogg_sync_pageout(&context->oy, &og) != 1) {
  141. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  142. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  143. return 1 ;
  144. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  145. }
  146. /* got a page. Feed it into the stream and get the packet */
  147. if(ogg_stream_pagein(&context->os, &og) != 0)
  148. return 1 ;
  149. }
  150. return 0 ;
  151. }
  152. static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
  153. {
  154. OggContext *context = avfcontext->priv_data;
  155. ogg_packet op ;
  156. char *buf ;
  157. ogg_page og ;
  158. AVStream *ast ;
  159. AVCodecContext *codec;
  160. uint8_t *p;
  161. int i;
  162. ogg_sync_init(&context->oy) ;
  163. buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
  164. if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
  165. return AVERROR_IO ;
  166. ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
  167. ogg_sync_pageout(&context->oy, &og) ;
  168. ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
  169. ogg_stream_pagein(&context->os, &og) ;
  170. /* currently only one vorbis stream supported */
  171. ast = av_new_stream(avfcontext, 0) ;
  172. if(!ast)
  173. return AVERROR_NOMEM ;
  174. av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
  175. codec= &ast->codec;
  176. codec->codec_type = CODEC_TYPE_AUDIO;
  177. codec->codec_id = CODEC_ID_VORBIS;
  178. for(i=0; i<3; i++){
  179. if(next_packet(avfcontext, &op)){
  180. return -1;
  181. }
  182. if(op.bytes >= (1<<16) || op.bytes < 0)
  183. return -1;
  184. codec->extradata_size+= 2 + op.bytes;
  185. codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  186. memset(codec->extradata + codec->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  187. p= codec->extradata + codec->extradata_size - 2 - op.bytes;
  188. *(p++)= op.bytes>>8;
  189. *(p++)= op.bytes&0xFF;
  190. memcpy(p, op.packet, op.bytes);
  191. }
  192. return 0 ;
  193. }
  194. static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
  195. ogg_packet op ;
  196. if(next_packet(avfcontext, &op))
  197. return AVERROR_IO ;
  198. if(av_new_packet(pkt, op.bytes) < 0)
  199. return AVERROR_IO ;
  200. pkt->stream_index = 0 ;
  201. memcpy(pkt->data, op.packet, op.bytes);
  202. if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1)
  203. pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate);
  204. // printf("%"PRId64" %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
  205. return op.bytes;
  206. }
  207. static int ogg_read_close(AVFormatContext *avfcontext) {
  208. OggContext *context = avfcontext->priv_data ;
  209. ogg_stream_clear(&context->os) ;
  210. ogg_sync_clear(&context->oy) ;
  211. return 0 ;
  212. }
  213. static AVInputFormat ogg_iformat = {
  214. "ogg",
  215. "Ogg Vorbis",
  216. sizeof(OggContext),
  217. NULL,
  218. ogg_read_header,
  219. ogg_read_packet,
  220. ogg_read_close,
  221. .extensions = "ogg",
  222. } ;
  223. #endif