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.

293 lines
10KB

  1. /*
  2. * Ogg muxer
  3. * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at free dot fr>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/crc.h"
  22. #include "libavcodec/xiph.h"
  23. #include "libavcodec/bytestream.h"
  24. #include "avformat.h"
  25. typedef struct {
  26. int64_t duration;
  27. unsigned page_counter;
  28. uint8_t *header[3];
  29. int header_len[3];
  30. /** for theora granule */
  31. int kfgshift;
  32. int64_t last_kf_pts;
  33. int vrev;
  34. int eos;
  35. } OGGStreamContext;
  36. static void ogg_update_checksum(AVFormatContext *s, int64_t crc_offset)
  37. {
  38. int64_t pos = url_ftell(s->pb);
  39. uint32_t checksum = get_checksum(s->pb);
  40. url_fseek(s->pb, crc_offset, SEEK_SET);
  41. put_be32(s->pb, checksum);
  42. url_fseek(s->pb, pos, SEEK_SET);
  43. }
  44. static int ogg_write_page(AVFormatContext *s, const uint8_t *data, int size,
  45. int64_t granule, int stream_index, int flags)
  46. {
  47. OGGStreamContext *oggstream = s->streams[stream_index]->priv_data;
  48. int64_t crc_offset;
  49. int page_segments, i;
  50. if (size >= 255*255) {
  51. granule = -1;
  52. size = 255*255;
  53. } else if (oggstream->eos)
  54. flags |= 4;
  55. page_segments = FFMIN((size/255)+!!size, 255);
  56. init_checksum(s->pb, ff_crc04C11DB7_update, 0);
  57. put_tag(s->pb, "OggS");
  58. put_byte(s->pb, 0);
  59. put_byte(s->pb, flags);
  60. put_le64(s->pb, granule);
  61. put_le32(s->pb, stream_index);
  62. put_le32(s->pb, oggstream->page_counter++);
  63. crc_offset = url_ftell(s->pb);
  64. put_le32(s->pb, 0); // crc
  65. put_byte(s->pb, page_segments);
  66. for (i = 0; i < page_segments-1; i++)
  67. put_byte(s->pb, 255);
  68. if (size) {
  69. put_byte(s->pb, size - (page_segments-1)*255);
  70. put_buffer(s->pb, data, size);
  71. }
  72. ogg_update_checksum(s, crc_offset);
  73. put_flush_packet(s->pb);
  74. return size;
  75. }
  76. static int ogg_build_flac_headers(const uint8_t *extradata, int extradata_size,
  77. OGGStreamContext *oggstream, int bitexact)
  78. {
  79. const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
  80. uint8_t *p;
  81. if (extradata_size != 34)
  82. return -1;
  83. oggstream->header_len[0] = 51;
  84. oggstream->header[0] = av_mallocz(51); // per ogg flac specs
  85. p = oggstream->header[0];
  86. bytestream_put_byte(&p, 0x7F);
  87. bytestream_put_buffer(&p, "FLAC", 4);
  88. bytestream_put_byte(&p, 1); // major version
  89. bytestream_put_byte(&p, 0); // minor version
  90. bytestream_put_be16(&p, 1); // headers packets without this one
  91. bytestream_put_buffer(&p, "fLaC", 4);
  92. bytestream_put_byte(&p, 0x00); // streaminfo
  93. bytestream_put_be24(&p, 34);
  94. bytestream_put_buffer(&p, extradata, 34);
  95. oggstream->header_len[1] = 1+3+4+strlen(vendor)+4;
  96. oggstream->header[1] = av_mallocz(oggstream->header_len[1]);
  97. p = oggstream->header[1];
  98. bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
  99. bytestream_put_be24(&p, oggstream->header_len[1] - 4);
  100. bytestream_put_le32(&p, strlen(vendor));
  101. bytestream_put_buffer(&p, vendor, strlen(vendor));
  102. bytestream_put_le32(&p, 0); // user comment list length
  103. return 0;
  104. }
  105. static int ogg_write_header(AVFormatContext *s)
  106. {
  107. OGGStreamContext *oggstream;
  108. int i, j;
  109. for (i = 0; i < s->nb_streams; i++) {
  110. AVStream *st = s->streams[i];
  111. if (st->codec->codec_type == CODEC_TYPE_AUDIO)
  112. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  113. else if (st->codec->codec_type == CODEC_TYPE_VIDEO)
  114. av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
  115. if (st->codec->codec_id != CODEC_ID_VORBIS &&
  116. st->codec->codec_id != CODEC_ID_THEORA &&
  117. st->codec->codec_id != CODEC_ID_FLAC) {
  118. av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
  119. return -1;
  120. }
  121. if (!st->codec->extradata || !st->codec->extradata_size) {
  122. av_log(s, AV_LOG_ERROR, "No extradata present\n");
  123. return -1;
  124. }
  125. oggstream = av_mallocz(sizeof(*oggstream));
  126. st->priv_data = oggstream;
  127. if (st->codec->codec_id == CODEC_ID_FLAC) {
  128. if (ogg_build_flac_headers(st->codec->extradata, st->codec->extradata_size,
  129. oggstream, st->codec->flags & CODEC_FLAG_BITEXACT) < 0) {
  130. av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
  131. av_freep(&st->priv_data);
  132. }
  133. } else {
  134. if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
  135. st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42,
  136. oggstream->header, oggstream->header_len) < 0) {
  137. av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
  138. av_freep(&st->priv_data);
  139. return -1;
  140. }
  141. if (st->codec->codec_id == CODEC_ID_THEORA) {
  142. /** KFGSHIFT is the width of the less significant section of the granule position
  143. The less significant section is the frame count since the last keyframe */
  144. oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
  145. oggstream->vrev = oggstream->header[0][9];
  146. av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
  147. oggstream->kfgshift, oggstream->vrev);
  148. }
  149. }
  150. }
  151. for (i = 0; i < 3; i++) {
  152. for (j = 0; j < s->nb_streams; j++) {
  153. AVStream *st = s->streams[j];
  154. OGGStreamContext *oggstream = st->priv_data;
  155. if (oggstream && oggstream->header_len[i]) {
  156. ogg_write_page(s, oggstream->header[i], oggstream->header_len[i],
  157. 0, st->index, i ? 0 : 2); // bos
  158. }
  159. }
  160. }
  161. return 0;
  162. }
  163. static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
  164. {
  165. AVStream *st = s->streams[pkt->stream_index];
  166. OGGStreamContext *oggstream = st->priv_data;
  167. uint8_t *ptr = pkt->data;
  168. int ret, size = pkt->size;
  169. int64_t granule;
  170. if (st->codec->codec_id == CODEC_ID_THEORA) {
  171. int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
  172. int pframe_count;
  173. if (pkt->flags & PKT_FLAG_KEY)
  174. oggstream->last_kf_pts = pts;
  175. pframe_count = pts - oggstream->last_kf_pts;
  176. // prevent frame count from overflow if key frame flag is not set
  177. if (pframe_count >= (1<<oggstream->kfgshift)) {
  178. oggstream->last_kf_pts += pframe_count;
  179. pframe_count = 0;
  180. }
  181. granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
  182. } else
  183. granule = pkt->pts + pkt->duration;
  184. oggstream->duration = granule;
  185. do {
  186. ret = ogg_write_page(s, ptr, size, granule, pkt->stream_index, ptr != pkt->data);
  187. ptr += ret; size -= ret;
  188. } while (size > 0 || ret == 255*255); // need to output a last nil page
  189. return 0;
  190. }
  191. int ogg_interleave_per_granule(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
  192. {
  193. AVPacketList *pktl, **next_point, *this_pktl;
  194. int stream_count = 0;
  195. int streams[MAX_STREAMS] = {0};
  196. int interleaved = 0;
  197. if (pkt) {
  198. AVStream *st = s->streams[pkt->stream_index];
  199. this_pktl = av_mallocz(sizeof(AVPacketList));
  200. this_pktl->pkt = *pkt;
  201. if (pkt->destruct == av_destruct_packet)
  202. pkt->destruct = NULL; // not shared -> must keep original from being freed
  203. else
  204. av_dup_packet(&this_pktl->pkt); // shared -> must dup
  205. next_point = &s->packet_buffer;
  206. while (*next_point) {
  207. AVStream *st2 = s->streams[(*next_point)->pkt.stream_index];
  208. AVPacket *next_pkt = &(*next_point)->pkt;
  209. int64_t cur_granule, next_granule;
  210. next_granule = av_rescale_q(next_pkt->pts + next_pkt->duration,
  211. st2->time_base, AV_TIME_BASE_Q);
  212. cur_granule = av_rescale_q(pkt->pts + pkt->duration,
  213. st->time_base, AV_TIME_BASE_Q);
  214. if (next_granule > cur_granule)
  215. break;
  216. next_point= &(*next_point)->next;
  217. }
  218. this_pktl->next= *next_point;
  219. *next_point= this_pktl;
  220. }
  221. pktl = s->packet_buffer;
  222. while (pktl) {
  223. if (streams[pktl->pkt.stream_index] == 0)
  224. stream_count++;
  225. streams[pktl->pkt.stream_index]++;
  226. // need to buffer at least one packet to set eos flag
  227. if (streams[pktl->pkt.stream_index] == 2)
  228. interleaved++;
  229. pktl = pktl->next;
  230. }
  231. if ((s->nb_streams == stream_count && interleaved == stream_count) ||
  232. (flush && stream_count)) {
  233. pktl= s->packet_buffer;
  234. *out= pktl->pkt;
  235. s->packet_buffer = pktl->next;
  236. if (flush && streams[out->stream_index] == 1) {
  237. OGGStreamContext *ogg = s->streams[out->stream_index]->priv_data;
  238. ogg->eos = 1;
  239. }
  240. av_freep(&pktl);
  241. return 1;
  242. } else {
  243. av_init_packet(out);
  244. return 0;
  245. }
  246. }
  247. static int ogg_write_trailer(AVFormatContext *s)
  248. {
  249. int i;
  250. for (i = 0; i < s->nb_streams; i++) {
  251. AVStream *st = s->streams[i];
  252. OGGStreamContext *oggstream = st->priv_data;
  253. if (st->codec->codec_id == CODEC_ID_FLAC) {
  254. av_free(oggstream->header[0]);
  255. av_free(oggstream->header[1]);
  256. }
  257. av_freep(&st->priv_data);
  258. }
  259. return 0;
  260. }
  261. AVOutputFormat ogg_muxer = {
  262. "ogg",
  263. NULL_IF_CONFIG_SMALL("Ogg"),
  264. "application/ogg",
  265. "ogg,ogv",
  266. 0,
  267. CODEC_ID_FLAC,
  268. CODEC_ID_THEORA,
  269. ogg_write_header,
  270. ogg_write_packet,
  271. ogg_write_trailer,
  272. .interleave_packet = ogg_interleave_per_granule,
  273. };