oggdec.c 15 KB

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