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.

626 lines
14KB

  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. #define MAX_PAGE_SIZE 65307
  31. #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
  32. static const struct ogg_codec * const ogg_codecs[] = {
  33. &ff_dirac_codec,
  34. &ff_speex_codec,
  35. &ff_vorbis_codec,
  36. &ff_theora_codec,
  37. &ff_flac_codec,
  38. &ff_old_dirac_codec,
  39. &ff_old_flac_codec,
  40. &ff_ogm_video_codec,
  41. &ff_ogm_audio_codec,
  42. &ff_ogm_text_codec,
  43. &ff_ogm_old_codec,
  44. NULL
  45. };
  46. //FIXME We could avoid some structure duplication
  47. static int
  48. ogg_save (AVFormatContext * s)
  49. {
  50. struct ogg *ogg = s->priv_data;
  51. struct ogg_state *ost =
  52. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  53. int i;
  54. ost->pos = url_ftell (s->pb);
  55. ost->curidx = ogg->curidx;
  56. ost->next = ogg->state;
  57. ost->nstreams = ogg->nstreams;
  58. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  59. for (i = 0; i < ogg->nstreams; i++){
  60. struct ogg_stream *os = ogg->streams + i;
  61. os->buf = av_malloc (os->bufsize);
  62. memset (os->buf, 0, os->bufsize);
  63. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  64. }
  65. ogg->state = ost;
  66. return 0;
  67. }
  68. static int
  69. ogg_restore (AVFormatContext * s, int discard)
  70. {
  71. struct ogg *ogg = s->priv_data;
  72. ByteIOContext *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. for (i = 0; i < ogg->nstreams; i++)
  80. av_free (ogg->streams[i].buf);
  81. url_fseek (bc, ost->pos, SEEK_SET);
  82. ogg->curidx = ost->curidx;
  83. ogg->nstreams = ost->nstreams;
  84. memcpy(ogg->streams, ost->streams,
  85. ost->nstreams * sizeof(*ogg->streams));
  86. }
  87. av_free (ost);
  88. return 0;
  89. }
  90. static int
  91. ogg_reset (struct ogg * ogg)
  92. {
  93. int i;
  94. for (i = 0; i < ogg->nstreams; i++){
  95. struct ogg_stream *os = ogg->streams + i;
  96. os->bufpos = 0;
  97. os->pstart = 0;
  98. os->psize = 0;
  99. os->granule = -1;
  100. os->lastpts = AV_NOPTS_VALUE;
  101. os->lastdts = AV_NOPTS_VALUE;
  102. os->nsegs = 0;
  103. os->segp = 0;
  104. os->incomplete = 0;
  105. }
  106. ogg->curidx = -1;
  107. return 0;
  108. }
  109. static const struct ogg_codec *
  110. ogg_find_codec (uint8_t * buf, int size)
  111. {
  112. int i;
  113. for (i = 0; ogg_codecs[i]; i++)
  114. if (size >= ogg_codecs[i]->magicsize &&
  115. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  116. return ogg_codecs[i];
  117. return NULL;
  118. }
  119. static int
  120. ogg_find_stream (struct ogg * ogg, int serial)
  121. {
  122. int i;
  123. for (i = 0; i < ogg->nstreams; i++)
  124. if (ogg->streams[i].serial == serial)
  125. return i;
  126. return -1;
  127. }
  128. static int
  129. ogg_new_stream (AVFormatContext * s, uint32_t serial)
  130. {
  131. struct ogg *ogg = s->priv_data;
  132. int idx = ogg->nstreams++;
  133. AVStream *st;
  134. struct ogg_stream *os;
  135. ogg->streams = av_realloc (ogg->streams,
  136. ogg->nstreams * sizeof (*ogg->streams));
  137. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  138. os = ogg->streams + idx;
  139. os->serial = serial;
  140. os->bufsize = DECODER_BUFFER_SIZE;
  141. os->buf = av_malloc(os->bufsize);
  142. os->header = -1;
  143. st = av_new_stream (s, idx);
  144. if (!st)
  145. return AVERROR(ENOMEM);
  146. av_set_pts_info(st, 64, 1, 1000000);
  147. return idx;
  148. }
  149. static int
  150. 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);
  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
  165. ogg_read_page (AVFormatContext * s, int *str)
  166. {
  167. ByteIOContext *bc = s->pb;
  168. struct ogg *ogg = s->priv_data;
  169. struct ogg_stream *os;
  170. int i = 0;
  171. int flags, nsegs;
  172. uint64_t gp;
  173. uint32_t serial;
  174. uint32_t seq;
  175. uint32_t crc;
  176. int size, idx;
  177. uint8_t sync[4];
  178. int sp = 0;
  179. if (get_buffer (bc, sync, 4) < 4)
  180. return -1;
  181. do{
  182. int c;
  183. if (sync[sp & 3] == 'O' &&
  184. sync[(sp + 1) & 3] == 'g' &&
  185. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  186. break;
  187. c = url_fgetc (bc);
  188. if (c < 0)
  189. return -1;
  190. sync[sp++ & 3] = c;
  191. }while (i++ < MAX_PAGE_SIZE);
  192. if (i >= MAX_PAGE_SIZE){
  193. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  194. return -1;
  195. }
  196. if (url_fgetc (bc) != 0) /* version */
  197. return -1;
  198. flags = url_fgetc (bc);
  199. gp = get_le64 (bc);
  200. serial = get_le32 (bc);
  201. seq = get_le32 (bc);
  202. crc = get_le32 (bc);
  203. nsegs = url_fgetc (bc);
  204. idx = ogg_find_stream (ogg, serial);
  205. if (idx < 0){
  206. idx = ogg_new_stream (s, serial);
  207. if (idx < 0)
  208. return -1;
  209. }
  210. os = ogg->streams + idx;
  211. if(os->psize > 0)
  212. ogg_new_buf(ogg, idx);
  213. if (get_buffer (bc, os->segments, nsegs) < nsegs)
  214. return -1;
  215. os->nsegs = nsegs;
  216. os->segp = 0;
  217. size = 0;
  218. for (i = 0; i < nsegs; i++)
  219. size += os->segments[i];
  220. if (flags & OGG_FLAG_CONT || os->incomplete){
  221. if (!os->psize){
  222. while (os->segp < os->nsegs){
  223. int seg = os->segments[os->segp++];
  224. os->pstart += seg;
  225. if (seg < 255)
  226. break;
  227. }
  228. }
  229. }else{
  230. os->psize = 0;
  231. }
  232. if (os->bufsize - os->bufpos < size){
  233. uint8_t *nb = av_malloc (os->bufsize *= 2);
  234. memcpy (nb, os->buf, os->bufpos);
  235. av_free (os->buf);
  236. os->buf = nb;
  237. }
  238. if (get_buffer (bc, os->buf + os->bufpos, size) < size)
  239. return -1;
  240. os->bufpos += size;
  241. os->granule = gp;
  242. os->flags = flags;
  243. if (str)
  244. *str = idx;
  245. return 0;
  246. }
  247. static int
  248. ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize)
  249. {
  250. struct ogg *ogg = s->priv_data;
  251. int idx, i;
  252. struct ogg_stream *os;
  253. int complete = 0;
  254. int segp = 0, psize = 0;
  255. #if 0
  256. av_log (s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
  257. #endif
  258. do{
  259. idx = ogg->curidx;
  260. while (idx < 0){
  261. if (ogg_read_page (s, &idx) < 0)
  262. return -1;
  263. }
  264. os = ogg->streams + idx;
  265. #if 0
  266. av_log (s, AV_LOG_DEBUG,
  267. "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  268. idx, os->pstart, os->psize, os->segp, os->nsegs);
  269. #endif
  270. if (!os->codec){
  271. if (os->header < 0){
  272. os->codec = ogg_find_codec (os->buf, os->bufpos);
  273. if (!os->codec){
  274. os->header = 0;
  275. return 0;
  276. }
  277. }else{
  278. return 0;
  279. }
  280. }
  281. segp = os->segp;
  282. psize = os->psize;
  283. while (os->segp < os->nsegs){
  284. int ss = os->segments[os->segp++];
  285. os->psize += ss;
  286. if (ss < 255){
  287. complete = 1;
  288. break;
  289. }
  290. }
  291. if (!complete && os->segp == os->nsegs){
  292. ogg->curidx = -1;
  293. os->incomplete = 1;
  294. }
  295. }while (!complete);
  296. #if 0
  297. av_log (s, AV_LOG_DEBUG,
  298. "ogg_packet: idx %i, frame size %i, start %i\n",
  299. idx, os->psize, os->pstart);
  300. #endif
  301. ogg->curidx = idx;
  302. os->incomplete = 0;
  303. if (!ogg->headers){
  304. int hdr = os->codec->header (s, idx);
  305. os->header = os->seq;
  306. if (!hdr){
  307. os->segp = segp;
  308. os->psize = psize;
  309. ogg->headers = 1;
  310. }else{
  311. os->pstart += os->psize;
  312. os->psize = 0;
  313. }
  314. }
  315. if (os->header > -1 && os->seq > os->header){
  316. os->pflags = 0;
  317. os->pduration = 0;
  318. if (os->codec && os->codec->packet)
  319. os->codec->packet (s, idx);
  320. if (str)
  321. *str = idx;
  322. if (dstart)
  323. *dstart = os->pstart;
  324. if (dsize)
  325. *dsize = os->psize;
  326. os->pstart += os->psize;
  327. os->psize = 0;
  328. }
  329. // determine whether there are more complete packets in this page
  330. // if not, the page's granule will apply to this packet
  331. os->page_end = 1;
  332. for (i = os->segp; i < os->nsegs; i++)
  333. if (os->segments[i] < 255) {
  334. os->page_end = 0;
  335. break;
  336. }
  337. os->seq++;
  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) < 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 uint64_t
  356. ogg_gptopts (AVFormatContext * s, int i, uint64_t gp, int64_t *dts)
  357. {
  358. struct ogg *ogg = s->priv_data;
  359. struct ogg_stream *os = ogg->streams + i;
  360. uint64_t pts = AV_NOPTS_VALUE;
  361. if(os->codec->gptopts){
  362. pts = os->codec->gptopts(s, i, gp, dts);
  363. } else {
  364. pts = gp;
  365. if (dts)
  366. *dts = pts;
  367. }
  368. return pts;
  369. }
  370. static int
  371. ogg_get_length (AVFormatContext * s)
  372. {
  373. struct ogg *ogg = s->priv_data;
  374. int idx = -1, i;
  375. int64_t size, end;
  376. if(url_is_streamed(s->pb))
  377. return 0;
  378. // already set
  379. if (s->duration != AV_NOPTS_VALUE)
  380. return 0;
  381. size = url_fsize(s->pb);
  382. if(size < 0)
  383. return 0;
  384. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
  385. ogg_save (s);
  386. url_fseek (s->pb, end, SEEK_SET);
  387. while (!ogg_read_page (s, &i)){
  388. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  389. ogg->streams[i].codec)
  390. idx = i;
  391. }
  392. if (idx != -1){
  393. s->streams[idx]->duration =
  394. ogg_gptopts (s, idx, ogg->streams[idx].granule, NULL);
  395. }
  396. ogg->size = size;
  397. ogg_restore (s, 0);
  398. return 0;
  399. }
  400. static int
  401. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  402. {
  403. struct ogg *ogg = s->priv_data;
  404. int i;
  405. ogg->curidx = -1;
  406. //linear headers seek from start
  407. if (ogg_get_headers (s) < 0){
  408. return -1;
  409. }
  410. for (i = 0; i < ogg->nstreams; i++)
  411. if (ogg->streams[i].header < 0)
  412. ogg->streams[i].codec = NULL;
  413. //linear granulepos seek from end
  414. ogg_get_length (s);
  415. //fill the extradata in the per codec callbacks
  416. return 0;
  417. }
  418. static int
  419. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  420. {
  421. struct ogg *ogg;
  422. struct ogg_stream *os;
  423. int idx = -1;
  424. int pstart, psize;
  425. //Get an ogg packet
  426. do{
  427. if (ogg_packet (s, &idx, &pstart, &psize) < 0)
  428. return AVERROR(EIO);
  429. }while (idx < 0 || !s->streams[idx]);
  430. ogg = s->priv_data;
  431. os = ogg->streams + idx;
  432. //Alloc a pkt
  433. if (av_new_packet (pkt, psize) < 0)
  434. return AVERROR(EIO);
  435. pkt->stream_index = idx;
  436. memcpy (pkt->data, os->buf + pstart, psize);
  437. if (os->lastpts != AV_NOPTS_VALUE) {
  438. pkt->pts = os->lastpts;
  439. os->lastpts = AV_NOPTS_VALUE;
  440. }
  441. if (os->lastdts != AV_NOPTS_VALUE) {
  442. pkt->dts = os->lastdts;
  443. os->lastdts = AV_NOPTS_VALUE;
  444. }
  445. if (os->page_end) {
  446. if (os->granule != -1LL) {
  447. if (os->codec && os->codec->granule_is_start)
  448. pkt->pts = ogg_gptopts(s, idx, os->granule, &pkt->dts);
  449. else
  450. os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
  451. os->granule = -1LL;
  452. } else
  453. av_log(s, AV_LOG_WARNING, "Packet is missing granule\n");
  454. }
  455. pkt->flags = os->pflags;
  456. pkt->duration = os->pduration;
  457. return psize;
  458. }
  459. static int
  460. ogg_read_close (AVFormatContext * s)
  461. {
  462. struct ogg *ogg = s->priv_data;
  463. int i;
  464. for (i = 0; i < ogg->nstreams; i++){
  465. av_free (ogg->streams[i].buf);
  466. av_free (ogg->streams[i].private);
  467. }
  468. av_free (ogg->streams);
  469. return 0;
  470. }
  471. static int64_t
  472. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  473. int64_t pos_limit)
  474. {
  475. struct ogg *ogg = s->priv_data;
  476. ByteIOContext *bc = s->pb;
  477. int64_t pts = AV_NOPTS_VALUE;
  478. int i;
  479. url_fseek(bc, *pos_arg, SEEK_SET);
  480. while (url_ftell(bc) < pos_limit && !ogg_read_page (s, &i)) {
  481. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  482. ogg->streams[i].codec && i == stream_index) {
  483. pts = ogg_gptopts(s, i, ogg->streams[i].granule, NULL);
  484. // FIXME: this is the position of the packet after the one with above
  485. // pts.
  486. *pos_arg = url_ftell(bc);
  487. break;
  488. }
  489. }
  490. ogg_reset(ogg);
  491. return pts;
  492. }
  493. static int ogg_probe(AVProbeData *p)
  494. {
  495. if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
  496. p->buf[2] == 'g' && p->buf[3] == 'S' &&
  497. p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
  498. return AVPROBE_SCORE_MAX;
  499. else
  500. return 0;
  501. }
  502. AVInputFormat ogg_demuxer = {
  503. "ogg",
  504. NULL_IF_CONFIG_SMALL("Ogg"),
  505. sizeof (struct ogg),
  506. ogg_probe,
  507. ogg_read_header,
  508. ogg_read_packet,
  509. ogg_read_close,
  510. NULL,
  511. ogg_read_timestamp,
  512. .extensions = "ogg",
  513. .metadata_conv = ff_vorbiscomment_metadata_conv,
  514. };