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.

701 lines
15KB

  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 "ogg2.h"
  29. #include "avformat.h"
  30. #define MAX_PAGE_SIZE 65307
  31. #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
  32. static ogg_codec_t *ogg_codecs[] = {
  33. &vorbis_codec,
  34. &theora_codec,
  35. &flac_codec,
  36. &ogm_video_codec,
  37. &ogm_audio_codec,
  38. &ogm_old_codec,
  39. NULL
  40. };
  41. #if 0 // CONFIG_MUXERS
  42. static int
  43. ogg_write_header (AVFormatContext * avfcontext)
  44. {
  45. }
  46. static int
  47. ogg_write_packet (AVFormatContext * avfcontext, AVPacket * pkt)
  48. {
  49. }
  50. static int
  51. ogg_write_trailer (AVFormatContext * avfcontext)
  52. {
  53. }
  54. AVOutputFormat ogg_muxer = {
  55. "ogg",
  56. "Ogg format",
  57. "application/ogg",
  58. "ogg",
  59. sizeof (OggContext),
  60. CODEC_ID_VORBIS,
  61. 0,
  62. ogg_write_header,
  63. ogg_write_packet,
  64. ogg_write_trailer,
  65. };
  66. #endif //CONFIG_MUXERS
  67. //FIXME We could avoid some structure duplication
  68. static int
  69. ogg_save (AVFormatContext * s)
  70. {
  71. ogg_t *ogg = s->priv_data;
  72. ogg_state_t *ost =
  73. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  74. int i;
  75. ost->pos = url_ftell (&s->pb);;
  76. ost->curidx = ogg->curidx;
  77. ost->next = ogg->state;
  78. ost->nstreams = ogg->nstreams;
  79. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  80. for (i = 0; i < ogg->nstreams; i++){
  81. ogg_stream_t *os = ogg->streams + i;
  82. os->buf = av_malloc (os->bufsize);
  83. memset (os->buf, 0, os->bufsize);
  84. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  85. }
  86. ogg->state = ost;
  87. return 0;
  88. }
  89. static int
  90. ogg_restore (AVFormatContext * s, int discard)
  91. {
  92. ogg_t *ogg = s->priv_data;
  93. ByteIOContext *bc = &s->pb;
  94. ogg_state_t *ost = ogg->state;
  95. int i;
  96. if (!ost)
  97. return 0;
  98. ogg->state = ost->next;
  99. if (!discard){
  100. for (i = 0; i < ogg->nstreams; i++)
  101. av_free (ogg->streams[i].buf);
  102. url_fseek (bc, ost->pos, SEEK_SET);
  103. ogg->curidx = ost->curidx;
  104. ogg->nstreams = ost->nstreams;
  105. memcpy(ogg->streams, ost->streams,
  106. ost->nstreams * sizeof(*ogg->streams));
  107. }
  108. av_free (ost);
  109. return 0;
  110. }
  111. static int
  112. ogg_reset (ogg_t * ogg)
  113. {
  114. int i;
  115. for (i = 0; i < ogg->nstreams; i++){
  116. ogg_stream_t *os = ogg->streams + i;
  117. os->bufpos = 0;
  118. os->pstart = 0;
  119. os->psize = 0;
  120. os->granule = -1;
  121. os->lastgp = -1;
  122. os->nsegs = 0;
  123. os->segp = 0;
  124. }
  125. ogg->curidx = -1;
  126. return 0;
  127. }
  128. static ogg_codec_t *
  129. ogg_find_codec (uint8_t * buf, int size)
  130. {
  131. int i;
  132. for (i = 0; ogg_codecs[i]; i++)
  133. if (size >= ogg_codecs[i]->magicsize &&
  134. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  135. return ogg_codecs[i];
  136. return NULL;
  137. }
  138. static int
  139. ogg_find_stream (ogg_t * ogg, int serial)
  140. {
  141. int i;
  142. for (i = 0; i < ogg->nstreams; i++)
  143. if (ogg->streams[i].serial == serial)
  144. return i;
  145. return -1;
  146. }
  147. static int
  148. ogg_new_stream (AVFormatContext * s, uint32_t serial)
  149. {
  150. ogg_t *ogg = s->priv_data;
  151. int idx = ogg->nstreams++;
  152. AVStream *st;
  153. ogg_stream_t *os;
  154. ogg->streams = av_realloc (ogg->streams,
  155. ogg->nstreams * sizeof (*ogg->streams));
  156. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  157. os = ogg->streams + idx;
  158. os->serial = serial;
  159. os->bufsize = DECODER_BUFFER_SIZE;
  160. os->buf = av_malloc(os->bufsize);
  161. os->header = -1;
  162. st = av_new_stream (s, idx);
  163. if (!st)
  164. return AVERROR_NOMEM;
  165. av_set_pts_info(st, 64, 1, 1000000);
  166. return idx;
  167. }
  168. static int
  169. ogg_new_buf(ogg_t *ogg, int idx)
  170. {
  171. ogg_stream_t *os = ogg->streams + idx;
  172. uint8_t *nb = av_malloc(os->bufsize);
  173. int size = os->bufpos - os->pstart;
  174. if(os->buf){
  175. memcpy(nb, os->buf + os->pstart, size);
  176. av_free(os->buf);
  177. }
  178. os->buf = nb;
  179. os->bufpos = size;
  180. os->pstart = 0;
  181. return 0;
  182. }
  183. static int
  184. ogg_read_page (AVFormatContext * s, int *str)
  185. {
  186. ByteIOContext *bc = &s->pb;
  187. ogg_t *ogg = s->priv_data;
  188. ogg_stream_t *os;
  189. int i = 0;
  190. int flags, nsegs;
  191. uint64_t gp;
  192. uint32_t serial;
  193. uint32_t seq;
  194. uint32_t crc;
  195. int size, idx;
  196. uint8_t sync[4];
  197. int sp = 0;
  198. if (get_buffer (bc, sync, 4) < 4)
  199. return -1;
  200. do{
  201. int c;
  202. if (sync[sp & 3] == 'O' &&
  203. sync[(sp + 1) & 3] == 'g' &&
  204. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  205. break;
  206. c = url_fgetc (bc);
  207. if (c < 0)
  208. return -1;
  209. sync[sp++ & 3] = c;
  210. }while (i++ < MAX_PAGE_SIZE);
  211. if (i >= MAX_PAGE_SIZE){
  212. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  213. return -1;
  214. }
  215. if (url_fgetc (bc) != 0) /* version */
  216. return -1;
  217. flags = url_fgetc (bc);
  218. gp = get_le64 (bc);
  219. serial = get_le32 (bc);
  220. seq = get_le32 (bc);
  221. crc = get_le32 (bc);
  222. nsegs = url_fgetc (bc);
  223. idx = ogg_find_stream (ogg, serial);
  224. if (idx < 0){
  225. idx = ogg_new_stream (s, serial);
  226. if (idx < 0)
  227. return -1;
  228. }
  229. os = ogg->streams + idx;
  230. if(os->psize > 0)
  231. ogg_new_buf(ogg, idx);
  232. if (get_buffer (bc, os->segments, nsegs) < nsegs)
  233. return -1;
  234. os->nsegs = nsegs;
  235. os->segp = 0;
  236. size = 0;
  237. for (i = 0; i < nsegs; i++)
  238. size += os->segments[i];
  239. if (flags & OGG_FLAG_CONT){
  240. if (!os->psize){
  241. while (os->segp < os->nsegs){
  242. int seg = os->segments[os->segp++];
  243. os->pstart += seg;
  244. if (seg < 255)
  245. break;
  246. }
  247. }
  248. }else{
  249. os->psize = 0;
  250. }
  251. if (os->bufsize - os->bufpos < size){
  252. uint8_t *nb = av_malloc (os->bufsize *= 2);
  253. memcpy (nb, os->buf, os->bufpos);
  254. av_free (os->buf);
  255. os->buf = nb;
  256. }
  257. if (get_buffer (bc, os->buf + os->bufpos, size) < size)
  258. return -1;
  259. os->lastgp = os->granule;
  260. os->bufpos += size;
  261. os->granule = gp;
  262. os->flags = flags;
  263. if (str)
  264. *str = idx;
  265. return 0;
  266. }
  267. static int
  268. ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize)
  269. {
  270. ogg_t *ogg = s->priv_data;
  271. int idx;
  272. ogg_stream_t *os;
  273. int complete = 0;
  274. int segp = 0, psize = 0;
  275. #if 0
  276. av_log (s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
  277. #endif
  278. do{
  279. idx = ogg->curidx;
  280. while (idx < 0){
  281. if (ogg_read_page (s, &idx) < 0)
  282. return -1;
  283. }
  284. os = ogg->streams + idx;
  285. #if 0
  286. av_log (s, AV_LOG_DEBUG,
  287. "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  288. idx, os->pstart, os->psize, os->segp, os->nsegs);
  289. #endif
  290. if (!os->codec){
  291. if (os->header < 0){
  292. os->codec = ogg_find_codec (os->buf, os->bufpos);
  293. if (!os->codec){
  294. os->header = 0;
  295. return 0;
  296. }
  297. }else{
  298. return 0;
  299. }
  300. }
  301. segp = os->segp;
  302. psize = os->psize;
  303. while (os->segp < os->nsegs){
  304. int ss = os->segments[os->segp++];
  305. os->psize += ss;
  306. if (ss < 255){
  307. complete = 1;
  308. break;
  309. }
  310. }
  311. if (!complete && os->segp == os->nsegs){
  312. ogg->curidx = -1;
  313. }
  314. }while (!complete);
  315. #if 0
  316. av_log (s, AV_LOG_DEBUG,
  317. "ogg_packet: idx %i, frame size %i, start %i\n",
  318. idx, os->psize, os->pstart);
  319. #endif
  320. ogg->curidx = idx;
  321. if (os->header < 0){
  322. int hdr = os->codec->header (s, idx);
  323. if (!hdr){
  324. os->header = os->seq;
  325. os->segp = segp;
  326. os->psize = psize;
  327. ogg->headers = 1;
  328. }else{
  329. os->pstart += os->psize;
  330. os->psize = 0;
  331. }
  332. }
  333. if (os->header > -1 && os->seq > os->header){
  334. if (os->codec && os->codec->packet)
  335. os->codec->packet (s, idx);
  336. if (str)
  337. *str = idx;
  338. if (dstart)
  339. *dstart = os->pstart;
  340. if (dsize)
  341. *dsize = os->psize;
  342. os->pstart += os->psize;
  343. os->psize = 0;
  344. }
  345. os->seq++;
  346. if (os->segp == os->nsegs)
  347. ogg->curidx = -1;
  348. return 0;
  349. }
  350. static int
  351. ogg_get_headers (AVFormatContext * s)
  352. {
  353. ogg_t *ogg = s->priv_data;
  354. do{
  355. if (ogg_packet (s, NULL, NULL, NULL) < 0)
  356. return -1;
  357. }while (!ogg->headers);
  358. #if 0
  359. av_log (s, AV_LOG_DEBUG, "found headers\n");
  360. #endif
  361. return 0;
  362. }
  363. static uint64_t
  364. ogg_gptopts (AVFormatContext * s, int i, uint64_t gp)
  365. {
  366. ogg_t *ogg = s->priv_data;
  367. ogg_stream_t *os = ogg->streams + i;
  368. uint64_t pts = AV_NOPTS_VALUE;
  369. if(os->codec->gptopts){
  370. pts = os->codec->gptopts(s, i, gp);
  371. } else {
  372. pts = gp;
  373. }
  374. return pts;
  375. }
  376. static int
  377. ogg_get_length (AVFormatContext * s)
  378. {
  379. ogg_t *ogg = s->priv_data;
  380. int idx = -1, i;
  381. offset_t size, end;
  382. if(s->pb.is_streamed)
  383. return 0;
  384. // already set
  385. if (s->duration != AV_NOPTS_VALUE)
  386. return 0;
  387. size = url_fsize(&s->pb);
  388. if(size < 0)
  389. return 0;
  390. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: size;
  391. ogg_save (s);
  392. url_fseek (&s->pb, end, SEEK_SET);
  393. while (!ogg_read_page (s, &i)){
  394. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  395. ogg->streams[i].codec)
  396. idx = i;
  397. }
  398. if (idx != -1){
  399. s->streams[idx]->duration =
  400. ogg_gptopts (s, idx, ogg->streams[idx].granule);
  401. }
  402. ogg->size = size;
  403. ogg_restore (s, 0);
  404. ogg_save (s);
  405. while (!ogg_read_page (s, &i)) {
  406. if (i == idx && ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
  407. break;
  408. }
  409. if (i == idx) {
  410. s->streams[idx]->start_time = ogg_gptopts (s, idx, ogg->streams[idx].granule);
  411. s->streams[idx]->duration -= s->streams[idx]->start_time;
  412. }
  413. ogg_restore (s, 0);
  414. return 0;
  415. }
  416. static int
  417. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  418. {
  419. ogg_t *ogg = s->priv_data;
  420. ogg->curidx = -1;
  421. //linear headers seek from start
  422. if (ogg_get_headers (s) < 0){
  423. return -1;
  424. }
  425. //linear granulepos seek from end
  426. ogg_get_length (s);
  427. //fill the extradata in the per codec callbacks
  428. return 0;
  429. }
  430. static int
  431. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  432. {
  433. ogg_t *ogg;
  434. ogg_stream_t *os;
  435. int idx = -1;
  436. int pstart, psize;
  437. //Get an ogg packet
  438. do{
  439. if (ogg_packet (s, &idx, &pstart, &psize) < 0)
  440. return AVERROR_IO;
  441. }while (idx < 0 || !s->streams[idx]);
  442. ogg = s->priv_data;
  443. os = ogg->streams + idx;
  444. //Alloc a pkt
  445. if (av_new_packet (pkt, psize) < 0)
  446. return AVERROR_IO;
  447. pkt->stream_index = idx;
  448. memcpy (pkt->data, os->buf + pstart, psize);
  449. if (os->lastgp != -1LL){
  450. pkt->pts = ogg_gptopts (s, idx, os->lastgp);
  451. os->lastgp = -1;
  452. }
  453. return psize;
  454. }
  455. static int
  456. ogg_read_close (AVFormatContext * s)
  457. {
  458. ogg_t *ogg = s->priv_data;
  459. int i;
  460. for (i = 0; i < ogg->nstreams; i++){
  461. av_free (ogg->streams[i].buf);
  462. av_free (ogg->streams[i].private);
  463. }
  464. av_free (ogg->streams);
  465. return 0;
  466. }
  467. static int
  468. ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
  469. int flags)
  470. {
  471. AVStream *st = s->streams[stream_index];
  472. ogg_t *ogg = s->priv_data;
  473. ByteIOContext *bc = &s->pb;
  474. uint64_t min = 0, max = ogg->size;
  475. uint64_t tmin = st->start_time, tmax = st->start_time + st->duration;
  476. int64_t pts = AV_NOPTS_VALUE;
  477. ogg_save (s);
  478. if ((uint64_t)target_ts < tmin || target_ts < 0)
  479. target_ts = tmin;
  480. while (min <= max && tmin < tmax){
  481. uint64_t p = min + (max - min) * (target_ts - tmin) / (tmax - tmin);
  482. int i = -1;
  483. url_fseek (bc, p, SEEK_SET);
  484. while (!ogg_read_page (s, &i)){
  485. if (i == stream_index && ogg->streams[i].granule != 0 &&
  486. ogg->streams[i].granule != -1)
  487. break;
  488. }
  489. if (i == -1)
  490. break;
  491. pts = ogg_gptopts (s, i, ogg->streams[i].granule);
  492. p = url_ftell (bc);
  493. if (FFABS (pts - target_ts) * st->time_base.num < st->time_base.den)
  494. break;
  495. if (pts > target_ts){
  496. if (max == p && tmax == pts) {
  497. // probably our tmin is wrong, causing us to always end up too late in the file
  498. tmin = (target_ts + tmin + 1) / 2;
  499. if (tmin == target_ts) {
  500. url_fseek(bc, min, SEEK_SET);
  501. break;
  502. }
  503. }
  504. max = p;
  505. tmax = pts;
  506. }else{
  507. if (min == p && tmin == pts) {
  508. // probably our tmax is wrong, causing us to always end up too early in the file
  509. tmax = (target_ts + tmax) / 2;
  510. if (tmax == target_ts) {
  511. url_fseek(bc, max, SEEK_SET);
  512. break;
  513. }
  514. }
  515. min = p;
  516. tmin = pts;
  517. }
  518. }
  519. if (FFABS (pts - target_ts) * st->time_base.num < st->time_base.den){
  520. ogg_restore (s, 1);
  521. ogg_reset (ogg);
  522. }else{
  523. ogg_restore (s, 0);
  524. pts = AV_NOPTS_VALUE;
  525. }
  526. av_update_cur_dts(s, st, pts);
  527. return 0;
  528. #if 0
  529. //later...
  530. int64_t pos;
  531. if (av_seek_frame_binary (s, stream_index, target_ts, flags) < 0)
  532. return -1;
  533. pos = url_ftell (&s->pb);
  534. ogg_read_timestamp (s, stream_index, &pos, pos - 1);
  535. #endif
  536. }
  537. #if 0
  538. static int64_t
  539. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  540. int64_t pos_limit)
  541. {
  542. ogg_t *ogg = s->priv_data;
  543. ByteIOContext *bc = &s->pb;
  544. int64_t pos, pts;
  545. if (*pos_arg < 0)
  546. return AV_NOPTS_VALUE;
  547. pos = *pos_arg;
  548. }
  549. #endif
  550. static int ogg_probe(AVProbeData *p)
  551. {
  552. if (p->buf_size < 6)
  553. return 0;
  554. if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
  555. p->buf[2] == 'g' && p->buf[3] == 'S' &&
  556. p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
  557. return AVPROBE_SCORE_MAX;
  558. else
  559. return 0;
  560. }
  561. AVInputFormat ogg_demuxer = {
  562. "ogg",
  563. "Ogg",
  564. sizeof (ogg_t),
  565. ogg_probe,
  566. ogg_read_header,
  567. ogg_read_packet,
  568. ogg_read_close,
  569. ogg_read_seek,
  570. // ogg_read_timestamp,
  571. .extensions = "ogg",
  572. };