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.

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