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.

667 lines
17KB

  1. /*
  2. * Ogg bitstream support
  3. * Luca Barbato <lu_zero@gentoo.org>
  4. * Based on tcvp implementation
  5. */
  6. /*
  7. Copyright (C) 2005 Michael Ahlberg, Måns Rullgård
  8. Permission is hereby granted, free of charge, to any person
  9. obtaining a copy of this software and associated documentation
  10. files (the "Software"), to deal in the Software without
  11. restriction, including without limitation the rights to use, copy,
  12. modify, merge, publish, distribute, sublicense, and/or sell copies
  13. of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be
  16. included in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. DEALINGS IN THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include "oggdec.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #include "vorbiscomment.h"
  31. #define MAX_PAGE_SIZE 65307
  32. #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
  33. static const struct ogg_codec * const ogg_codecs[] = {
  34. &ff_skeleton_codec,
  35. &ff_dirac_codec,
  36. &ff_speex_codec,
  37. &ff_vorbis_codec,
  38. &ff_theora_codec,
  39. &ff_flac_codec,
  40. &ff_celt_codec,
  41. &ff_old_dirac_codec,
  42. &ff_old_flac_codec,
  43. &ff_ogm_video_codec,
  44. &ff_ogm_audio_codec,
  45. &ff_ogm_text_codec,
  46. &ff_ogm_old_codec,
  47. NULL
  48. };
  49. //FIXME We could avoid some structure duplication
  50. static int ogg_save(AVFormatContext *s)
  51. {
  52. struct ogg *ogg = s->priv_data;
  53. struct ogg_state *ost =
  54. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  55. int i;
  56. ost->pos = avio_tell (s->pb);
  57. ost->curidx = ogg->curidx;
  58. ost->next = ogg->state;
  59. ost->nstreams = ogg->nstreams;
  60. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  61. for (i = 0; i < ogg->nstreams; i++){
  62. struct ogg_stream *os = ogg->streams + i;
  63. os->buf = av_mallocz (os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  64. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  65. }
  66. ogg->state = ost;
  67. return 0;
  68. }
  69. static int ogg_restore(AVFormatContext *s, int discard)
  70. {
  71. struct ogg *ogg = s->priv_data;
  72. AVIOContext *bc = s->pb;
  73. struct ogg_state *ost = ogg->state;
  74. int i;
  75. if (!ost)
  76. return 0;
  77. ogg->state = ost->next;
  78. if (!discard){
  79. struct ogg_stream *old_streams = ogg->streams;
  80. for (i = 0; i < ogg->nstreams; i++)
  81. av_free (ogg->streams[i].buf);
  82. avio_seek (bc, ost->pos, SEEK_SET);
  83. ogg->curidx = ost->curidx;
  84. ogg->nstreams = ost->nstreams;
  85. ogg->streams = av_realloc (ogg->streams,
  86. ogg->nstreams * sizeof (*ogg->streams));
  87. if (ogg->streams) {
  88. memcpy(ogg->streams, ost->streams,
  89. ost->nstreams * sizeof(*ogg->streams));
  90. } else {
  91. av_free(old_streams);
  92. ogg->nstreams = 0;
  93. }
  94. }
  95. av_free (ost);
  96. return 0;
  97. }
  98. static int ogg_reset(struct ogg *ogg)
  99. {
  100. int i;
  101. for (i = 0; i < ogg->nstreams; i++){
  102. struct ogg_stream *os = ogg->streams + i;
  103. os->bufpos = 0;
  104. os->pstart = 0;
  105. os->psize = 0;
  106. os->granule = -1;
  107. os->lastpts = AV_NOPTS_VALUE;
  108. os->lastdts = AV_NOPTS_VALUE;
  109. os->sync_pos = -1;
  110. os->page_pos = 0;
  111. os->nsegs = 0;
  112. os->segp = 0;
  113. os->incomplete = 0;
  114. }
  115. ogg->curidx = -1;
  116. return 0;
  117. }
  118. static const struct ogg_codec *ogg_find_codec(uint8_t *buf, int size)
  119. {
  120. int i;
  121. for (i = 0; ogg_codecs[i]; i++)
  122. if (size >= ogg_codecs[i]->magicsize &&
  123. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  124. return ogg_codecs[i];
  125. return NULL;
  126. }
  127. static int ogg_new_stream(AVFormatContext *s, uint32_t serial, int new_avstream)
  128. {
  129. struct ogg *ogg = s->priv_data;
  130. int idx = ogg->nstreams++;
  131. AVStream *st;
  132. struct ogg_stream *os;
  133. ogg->streams = av_realloc (ogg->streams,
  134. ogg->nstreams * sizeof (*ogg->streams));
  135. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  136. os = ogg->streams + idx;
  137. os->serial = serial;
  138. os->bufsize = DECODER_BUFFER_SIZE;
  139. os->buf = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  140. os->header = -1;
  141. if (new_avstream) {
  142. st = avformat_new_stream(s, NULL);
  143. if (!st)
  144. return AVERROR(ENOMEM);
  145. st->id = idx;
  146. avpriv_set_pts_info(st, 64, 1, 1000000);
  147. }
  148. return idx;
  149. }
  150. static int ogg_new_buf(struct ogg *ogg, int idx)
  151. {
  152. struct ogg_stream *os = ogg->streams + idx;
  153. uint8_t *nb = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  154. int size = os->bufpos - os->pstart;
  155. if(os->buf){
  156. memcpy(nb, os->buf + os->pstart, size);
  157. av_free(os->buf);
  158. }
  159. os->buf = nb;
  160. os->bufpos = size;
  161. os->pstart = 0;
  162. return 0;
  163. }
  164. static int ogg_read_page(AVFormatContext *s, int *str)
  165. {
  166. AVIOContext *bc = s->pb;
  167. struct ogg *ogg = s->priv_data;
  168. struct ogg_stream *os;
  169. int ret, i = 0;
  170. int flags, nsegs;
  171. uint64_t gp;
  172. uint32_t serial;
  173. int size, idx;
  174. uint8_t sync[4];
  175. int sp = 0;
  176. ret = avio_read(bc, sync, 4);
  177. if (ret < 4)
  178. return ret < 0 ? ret : AVERROR_EOF;
  179. do{
  180. int c;
  181. if (sync[sp & 3] == 'O' &&
  182. sync[(sp + 1) & 3] == 'g' &&
  183. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  184. break;
  185. c = avio_r8(bc);
  186. if (bc->eof_reached)
  187. return AVERROR_EOF;
  188. sync[sp++ & 3] = c;
  189. }while (i++ < MAX_PAGE_SIZE);
  190. if (i >= MAX_PAGE_SIZE){
  191. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  192. return AVERROR_INVALIDDATA;
  193. }
  194. if (avio_r8(bc) != 0) /* version */
  195. return AVERROR_INVALIDDATA;
  196. flags = avio_r8(bc);
  197. gp = avio_rl64 (bc);
  198. serial = avio_rl32 (bc);
  199. avio_skip(bc, 8); /* seq, crc */
  200. nsegs = avio_r8(bc);
  201. idx = ogg_find_stream (ogg, serial);
  202. if (idx < 0){
  203. if (ogg->headers) {
  204. int n;
  205. for (n = 0; n < ogg->nstreams; n++) {
  206. av_freep(&ogg->streams[n].buf);
  207. if (!ogg->state || ogg->state->streams[n].private != ogg->streams[n].private)
  208. av_freep(&ogg->streams[n].private);
  209. }
  210. ogg->curidx = -1;
  211. ogg->nstreams = 0;
  212. idx = ogg_new_stream(s, serial, 0);
  213. } else {
  214. idx = ogg_new_stream(s, serial, 1);
  215. }
  216. if (idx < 0)
  217. return idx;
  218. }
  219. os = ogg->streams + idx;
  220. os->page_pos = avio_tell(bc) - 27;
  221. if(os->psize > 0)
  222. ogg_new_buf(ogg, idx);
  223. ret = avio_read(bc, os->segments, nsegs);
  224. if (ret < nsegs)
  225. return ret < 0 ? ret : AVERROR_EOF;
  226. os->nsegs = nsegs;
  227. os->segp = 0;
  228. size = 0;
  229. for (i = 0; i < nsegs; i++)
  230. size += os->segments[i];
  231. if (flags & OGG_FLAG_CONT || os->incomplete){
  232. if (!os->psize){
  233. while (os->segp < os->nsegs){
  234. int seg = os->segments[os->segp++];
  235. os->pstart += seg;
  236. if (seg < 255)
  237. break;
  238. }
  239. os->sync_pos = os->page_pos;
  240. }
  241. }else{
  242. os->psize = 0;
  243. os->sync_pos = os->page_pos;
  244. }
  245. if (os->bufsize - os->bufpos < size){
  246. uint8_t *nb = av_malloc ((os->bufsize *= 2) + FF_INPUT_BUFFER_PADDING_SIZE);
  247. memcpy (nb, os->buf, os->bufpos);
  248. av_free (os->buf);
  249. os->buf = nb;
  250. }
  251. ret = avio_read(bc, os->buf + os->bufpos, size);
  252. if (ret < size)
  253. return ret < 0 ? ret : AVERROR_EOF;
  254. os->bufpos += size;
  255. os->granule = gp;
  256. os->flags = flags;
  257. memset(os->buf + os->bufpos, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  258. if (str)
  259. *str = idx;
  260. return 0;
  261. }
  262. static int ogg_packet(AVFormatContext *s, int *str, int *dstart, int *dsize,
  263. int64_t *fpos)
  264. {
  265. struct ogg *ogg = s->priv_data;
  266. int idx, i, ret;
  267. struct ogg_stream *os;
  268. int complete = 0;
  269. int segp = 0, psize = 0;
  270. av_dlog(s, "ogg_packet: curidx=%i\n", ogg->curidx);
  271. do{
  272. idx = ogg->curidx;
  273. while (idx < 0){
  274. ret = ogg_read_page(s, &idx);
  275. if (ret < 0)
  276. return ret;
  277. }
  278. os = ogg->streams + idx;
  279. av_dlog(s, "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  280. idx, os->pstart, os->psize, os->segp, os->nsegs);
  281. if (!os->codec){
  282. if (os->header < 0){
  283. os->codec = ogg_find_codec (os->buf, os->bufpos);
  284. if (!os->codec){
  285. av_log(s, AV_LOG_WARNING, "Codec not found\n");
  286. os->header = 0;
  287. return 0;
  288. }
  289. }else{
  290. return 0;
  291. }
  292. }
  293. segp = os->segp;
  294. psize = os->psize;
  295. while (os->segp < os->nsegs){
  296. int ss = os->segments[os->segp++];
  297. os->psize += ss;
  298. if (ss < 255){
  299. complete = 1;
  300. break;
  301. }
  302. }
  303. if (!complete && os->segp == os->nsegs){
  304. ogg->curidx = -1;
  305. os->incomplete = 1;
  306. }
  307. }while (!complete);
  308. av_dlog(s, "ogg_packet: idx %i, frame size %i, start %i\n",
  309. idx, os->psize, os->pstart);
  310. if (os->granule == -1)
  311. av_log(s, AV_LOG_WARNING, "Page at %"PRId64" is missing granule\n", os->page_pos);
  312. ogg->curidx = idx;
  313. os->incomplete = 0;
  314. if (os->header) {
  315. os->header = os->codec->header (s, idx);
  316. if (!os->header){
  317. os->segp = segp;
  318. os->psize = psize;
  319. // We have reached the first non-header packet in this stream.
  320. // Unfortunately more header packets may still follow for others,
  321. // but if we continue with header parsing we may lose data packets.
  322. ogg->headers = 1;
  323. // Update the header state for all streams and
  324. // compute the data_offset.
  325. if (!s->data_offset)
  326. s->data_offset = os->sync_pos;
  327. for (i = 0; i < ogg->nstreams; i++) {
  328. struct ogg_stream *cur_os = ogg->streams + i;
  329. // if we have a partial non-header packet, its start is
  330. // obviously at or after the data start
  331. if (cur_os->incomplete)
  332. s->data_offset = FFMIN(s->data_offset, cur_os->sync_pos);
  333. }
  334. }else{
  335. os->pstart += os->psize;
  336. os->psize = 0;
  337. }
  338. } else {
  339. os->pflags = 0;
  340. os->pduration = 0;
  341. if (os->codec && os->codec->packet)
  342. os->codec->packet (s, idx);
  343. if (str)
  344. *str = idx;
  345. if (dstart)
  346. *dstart = os->pstart;
  347. if (dsize)
  348. *dsize = os->psize;
  349. if (fpos)
  350. *fpos = os->sync_pos;
  351. os->pstart += os->psize;
  352. os->psize = 0;
  353. os->sync_pos = os->page_pos;
  354. }
  355. // determine whether there are more complete packets in this page
  356. // if not, the page's granule will apply to this packet
  357. os->page_end = 1;
  358. for (i = os->segp; i < os->nsegs; i++)
  359. if (os->segments[i] < 255) {
  360. os->page_end = 0;
  361. break;
  362. }
  363. if (os->segp == os->nsegs)
  364. ogg->curidx = -1;
  365. return 0;
  366. }
  367. static int ogg_get_headers(AVFormatContext *s)
  368. {
  369. struct ogg *ogg = s->priv_data;
  370. int ret;
  371. do{
  372. ret = ogg_packet(s, NULL, NULL, NULL, NULL);
  373. if (ret < 0)
  374. return ret;
  375. }while (!ogg->headers);
  376. av_dlog(s, "found headers\n");
  377. return 0;
  378. }
  379. static int ogg_get_length(AVFormatContext *s)
  380. {
  381. struct ogg *ogg = s->priv_data;
  382. int i;
  383. int64_t size, end;
  384. if(!s->pb->seekable)
  385. return 0;
  386. // already set
  387. if (s->duration != AV_NOPTS_VALUE)
  388. return 0;
  389. size = avio_size(s->pb);
  390. if(size < 0)
  391. return 0;
  392. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
  393. ogg_save (s);
  394. avio_seek (s->pb, end, SEEK_SET);
  395. while (!ogg_read_page (s, &i)){
  396. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  397. ogg->streams[i].codec) {
  398. s->streams[i]->duration =
  399. ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
  400. if (s->streams[i]->start_time != AV_NOPTS_VALUE)
  401. s->streams[i]->duration -= s->streams[i]->start_time;
  402. }
  403. }
  404. ogg_restore (s, 0);
  405. return 0;
  406. }
  407. static int ogg_read_header(AVFormatContext *s)
  408. {
  409. struct ogg *ogg = s->priv_data;
  410. int ret, i;
  411. ogg->curidx = -1;
  412. //linear headers seek from start
  413. ret = ogg_get_headers(s);
  414. if (ret < 0)
  415. return ret;
  416. for (i = 0; i < ogg->nstreams; i++)
  417. if (ogg->streams[i].header < 0)
  418. ogg->streams[i].codec = NULL;
  419. //linear granulepos seek from end
  420. ogg_get_length (s);
  421. //fill the extradata in the per codec callbacks
  422. return 0;
  423. }
  424. static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
  425. {
  426. struct ogg *ogg = s->priv_data;
  427. struct ogg_stream *os = ogg->streams + idx;
  428. int64_t pts = AV_NOPTS_VALUE;
  429. if (dts)
  430. *dts = AV_NOPTS_VALUE;
  431. if (os->lastpts != AV_NOPTS_VALUE) {
  432. pts = os->lastpts;
  433. os->lastpts = AV_NOPTS_VALUE;
  434. }
  435. if (os->lastdts != AV_NOPTS_VALUE) {
  436. if (dts)
  437. *dts = os->lastdts;
  438. os->lastdts = AV_NOPTS_VALUE;
  439. }
  440. if (os->page_end) {
  441. if (os->granule != -1LL) {
  442. if (os->codec && os->codec->granule_is_start)
  443. pts = ogg_gptopts(s, idx, os->granule, dts);
  444. else
  445. os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
  446. os->granule = -1LL;
  447. }
  448. }
  449. return pts;
  450. }
  451. static int ogg_read_packet(AVFormatContext *s, AVPacket *pkt)
  452. {
  453. struct ogg *ogg;
  454. struct ogg_stream *os;
  455. int idx = -1, ret;
  456. int pstart, psize;
  457. int64_t fpos, pts, dts;
  458. //Get an ogg packet
  459. retry:
  460. do{
  461. ret = ogg_packet(s, &idx, &pstart, &psize, &fpos);
  462. if (ret < 0)
  463. return ret;
  464. }while (idx < 0 || !s->streams[idx]);
  465. ogg = s->priv_data;
  466. os = ogg->streams + idx;
  467. // pflags might not be set until after this
  468. pts = ogg_calc_pts(s, idx, &dts);
  469. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  470. goto retry;
  471. os->keyframe_seek = 0;
  472. //Alloc a pkt
  473. ret = av_new_packet(pkt, psize);
  474. if (ret < 0)
  475. return ret;
  476. pkt->stream_index = idx;
  477. memcpy (pkt->data, os->buf + pstart, psize);
  478. pkt->pts = pts;
  479. pkt->dts = dts;
  480. pkt->flags = os->pflags;
  481. pkt->duration = os->pduration;
  482. pkt->pos = fpos;
  483. return psize;
  484. }
  485. static int ogg_read_close(AVFormatContext *s)
  486. {
  487. struct ogg *ogg = s->priv_data;
  488. int i;
  489. for (i = 0; i < ogg->nstreams; i++){
  490. av_free (ogg->streams[i].buf);
  491. av_free (ogg->streams[i].private);
  492. }
  493. av_free (ogg->streams);
  494. return 0;
  495. }
  496. static int64_t ogg_read_timestamp(AVFormatContext *s, int stream_index,
  497. int64_t *pos_arg, int64_t pos_limit)
  498. {
  499. struct ogg *ogg = s->priv_data;
  500. AVIOContext *bc = s->pb;
  501. int64_t pts = AV_NOPTS_VALUE;
  502. int i = -1;
  503. avio_seek(bc, *pos_arg, SEEK_SET);
  504. ogg_reset(ogg);
  505. while (avio_tell(bc) < pos_limit && !ogg_packet(s, &i, NULL, NULL, pos_arg)) {
  506. if (i == stream_index) {
  507. struct ogg_stream *os = ogg->streams + stream_index;
  508. pts = ogg_calc_pts(s, i, NULL);
  509. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  510. pts = AV_NOPTS_VALUE;
  511. }
  512. if (pts != AV_NOPTS_VALUE)
  513. break;
  514. }
  515. ogg_reset(ogg);
  516. return pts;
  517. }
  518. static int ogg_read_seek(AVFormatContext *s, int stream_index,
  519. int64_t timestamp, int flags)
  520. {
  521. struct ogg *ogg = s->priv_data;
  522. struct ogg_stream *os = ogg->streams + stream_index;
  523. int ret;
  524. // Try seeking to a keyframe first. If this fails (very possible),
  525. // av_seek_frame will fall back to ignoring keyframes
  526. if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO
  527. && !(flags & AVSEEK_FLAG_ANY))
  528. os->keyframe_seek = 1;
  529. ret = ff_seek_frame_binary(s, stream_index, timestamp, flags);
  530. os = ogg->streams + stream_index;
  531. if (ret < 0)
  532. os->keyframe_seek = 0;
  533. return ret;
  534. }
  535. static int ogg_probe(AVProbeData *p)
  536. {
  537. if (!memcmp("OggS", p->buf, 5) && p->buf[5] <= 0x7)
  538. return AVPROBE_SCORE_MAX;
  539. return 0;
  540. }
  541. AVInputFormat ff_ogg_demuxer = {
  542. .name = "ogg",
  543. .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
  544. .priv_data_size = sizeof(struct ogg),
  545. .read_probe = ogg_probe,
  546. .read_header = ogg_read_header,
  547. .read_packet = ogg_read_packet,
  548. .read_close = ogg_read_close,
  549. .read_seek = ogg_read_seek,
  550. .read_timestamp = ogg_read_timestamp,
  551. .extensions = "ogg",
  552. .flags = AVFMT_GENERIC_INDEX,
  553. };