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.

647 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 "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. NULL
  36. };
  37. #if 0 // CONFIG_ENCODERS
  38. static int
  39. ogg_write_header (AVFormatContext * avfcontext)
  40. {
  41. }
  42. static int
  43. ogg_write_packet (AVFormatContext * avfcontext, AVPacket * pkt)
  44. {
  45. }
  46. static int
  47. ogg_write_trailer (AVFormatContext * avfcontext)
  48. {
  49. }
  50. static AVOutputFormat ogg_oformat = {
  51. "ogg",
  52. "Ogg Vorbis",
  53. "audio/x-vorbis",
  54. "ogg",
  55. sizeof (OggContext),
  56. CODEC_ID_VORBIS,
  57. 0,
  58. ogg_write_header,
  59. ogg_write_packet,
  60. ogg_write_trailer,
  61. };
  62. #endif //CONFIG_ENCODERS
  63. //FIXME We could avoid some structure duplication
  64. static int
  65. ogg_save (AVFormatContext * s)
  66. {
  67. ogg_t *ogg = s->priv_data;
  68. ogg_state_t *ost =
  69. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  70. int i;
  71. ost->pos = url_ftell (&s->pb);;
  72. ost->curidx = ogg->curidx;
  73. ost->next = ogg->state;
  74. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  75. for (i = 0; i < ogg->nstreams; i++){
  76. ogg_stream_t *os = ogg->streams + i;
  77. os->buf = av_malloc (os->bufsize);
  78. memset (os->buf, 0, os->bufsize);
  79. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  80. }
  81. ogg->state = ost;
  82. return 0;
  83. }
  84. static int
  85. ogg_restore (AVFormatContext * s, int discard)
  86. {
  87. ogg_t *ogg = s->priv_data;
  88. ByteIOContext *bc = &s->pb;
  89. ogg_state_t *ost = ogg->state;
  90. int i;
  91. if (!ost)
  92. return 0;
  93. ogg->state = ost->next;
  94. if (!discard){
  95. for (i = 0; i < ogg->nstreams; i++)
  96. av_free (ogg->streams[i].buf);
  97. url_fseek (bc, ost->pos, SEEK_SET);
  98. ogg->curidx = ost->curidx;
  99. memcpy (ogg->streams, ost->streams,
  100. ogg->nstreams * sizeof (*ogg->streams));
  101. }
  102. av_free (ost);
  103. return 0;
  104. }
  105. static int
  106. ogg_reset (ogg_t * ogg)
  107. {
  108. int i;
  109. for (i = 0; i < ogg->nstreams; i++){
  110. ogg_stream_t *os = ogg->streams + i;
  111. os->bufpos = 0;
  112. os->pstart = 0;
  113. os->psize = 0;
  114. os->granule = -1;
  115. os->lastgp = -1;
  116. os->nsegs = 0;
  117. os->segp = 0;
  118. }
  119. ogg->curidx = -1;
  120. return 0;
  121. }
  122. static ogg_codec_t *
  123. ogg_find_codec (uint8_t * buf, int size)
  124. {
  125. int i;
  126. for (i = 0; ogg_codecs[i]; i++)
  127. if (size >= ogg_codecs[i]->magicsize &&
  128. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  129. return ogg_codecs[i];
  130. return NULL;
  131. }
  132. static int
  133. ogg_find_stream (ogg_t * ogg, int serial)
  134. {
  135. int i;
  136. for (i = 0; i < ogg->nstreams; i++)
  137. if (ogg->streams[i].serial == serial)
  138. return i;
  139. return -1;
  140. }
  141. static int
  142. ogg_new_stream (AVFormatContext * s, uint32_t serial)
  143. {
  144. ogg_t *ogg = s->priv_data;
  145. int idx = ogg->nstreams++;
  146. AVStream *st;
  147. ogg_stream_t *os;
  148. ogg->streams = av_realloc (ogg->streams,
  149. ogg->nstreams * sizeof (*ogg->streams));
  150. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  151. os = ogg->streams + idx;
  152. os->serial = serial;
  153. os->bufsize = DECODER_BUFFER_SIZE;
  154. os->buf = av_malloc (os->bufsize);
  155. memset (os->buf, 0, os->bufsize);
  156. os->header = -1;
  157. st = av_new_stream (s, idx);
  158. if (!st)
  159. return AVERROR_NOMEM;
  160. av_set_pts_info(st, 64, 1, 1000000);
  161. st->start_time = 0;
  162. return idx;
  163. }
  164. static int
  165. ogg_read_page (AVFormatContext * s, int *str)
  166. {
  167. ByteIOContext *bc = &s->pb;
  168. ogg_t *ogg = s->priv_data;
  169. ogg_stream_t *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. char 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 (get_buffer (bc, os->segments, nsegs) < nsegs)
  212. return -1;
  213. os->nsegs = nsegs;
  214. os->segp = 0;
  215. size = 0;
  216. for (i = 0; i < nsegs; i++)
  217. size += os->segments[i];
  218. if (flags & OGG_FLAG_CONT){
  219. if (!os->psize){
  220. while (os->segp < os->nsegs){
  221. int seg = os->segments[os->segp++];
  222. os->pstart += seg;
  223. if (seg < 255)
  224. break;
  225. }
  226. }
  227. }else{
  228. os->psize = 0;
  229. }
  230. if (os->bufsize - os->bufpos < size){
  231. uint8_t *nb = av_malloc (os->bufsize *= 2);
  232. memset (nb, 0, os->bufsize);
  233. memcpy (nb, os->buf, os->bufpos);
  234. av_free (os->buf);
  235. os->buf = nb;
  236. }
  237. if (get_buffer (bc, os->buf + os->bufpos, size) < size)
  238. return -1;
  239. os->lastgp = os->granule;
  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)
  249. {
  250. ogg_t *ogg = s->priv_data;
  251. int idx;
  252. ogg_stream_t *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. uint8_t *nb = av_malloc (os->bufsize);
  293. int size = os->bufpos - os->pstart;
  294. memset (nb, 0, os->bufsize);
  295. memcpy (nb, os->buf + os->pstart, size);
  296. av_free (os->buf);
  297. os->buf = nb;
  298. os->bufpos = size;
  299. os->pstart = 0;
  300. ogg->curidx = -1;
  301. }
  302. }while (!complete);
  303. #if 0
  304. av_log (s, AV_LOG_DEBUG,
  305. "ogg_packet: idx %i, frame size %i, start %i\n",
  306. idx, os->psize, os->pstart);
  307. #endif
  308. ogg->curidx = idx;
  309. if (os->header < 0){
  310. int hdr = os->codec->header (s, idx);
  311. if (!hdr){
  312. os->header = os->seq;
  313. os->segp = segp;
  314. os->psize = psize;
  315. ogg->headers = 1;
  316. }else{
  317. os->pstart += os->psize;
  318. os->psize = 0;
  319. }
  320. }
  321. if (os->header > -1 && os->seq > os->header){
  322. if (os->codec && os->codec->packet)
  323. os->codec->packet (s, idx);
  324. if (str)
  325. *str = idx;
  326. }
  327. os->seq++;
  328. if (os->segp == os->nsegs)
  329. ogg->curidx = -1;
  330. return 0;
  331. }
  332. static int
  333. ogg_get_headers (AVFormatContext * s)
  334. {
  335. ogg_t *ogg = s->priv_data;
  336. do{
  337. if (ogg_packet (s, NULL) < 0)
  338. return -1;
  339. }while (!ogg->headers);
  340. #if 0
  341. av_log (s, AV_LOG_DEBUG, "found headers\n");
  342. #endif
  343. return 0;
  344. }
  345. static uint64_t
  346. ogg_gptopts (AVFormatContext * s, int i, uint64_t gp)
  347. {
  348. ogg_t *ogg = s->priv_data;
  349. ogg_stream_t *os = ogg->streams + i;
  350. AVStream *st = s->streams[i];
  351. AVCodecContext *codec = &st->codec;
  352. uint64_t pts = AV_NOPTS_VALUE;
  353. if(os->codec->gptopts){
  354. pts = os->codec->gptopts(s, i, gp);
  355. } else if (codec->codec_type == CODEC_TYPE_AUDIO){
  356. pts = gp * 1000000LL / codec->sample_rate;
  357. }else if (codec->codec_type == CODEC_TYPE_VIDEO){
  358. pts = gp;
  359. }
  360. return pts;
  361. }
  362. static int
  363. ogg_get_length (AVFormatContext * s)
  364. {
  365. ogg_t *ogg = s->priv_data;
  366. URLContext *h = url_fileno (&s->pb);
  367. int idx = -1, i;
  368. //FIXME: get the right ctx flag to know if is seekable or not
  369. // if(ogg->f->flags & URL_FLAG_STREAMED)
  370. // return 0;
  371. // already set
  372. if (s->duration != AV_NOPTS_VALUE)
  373. return 0;
  374. ogg_save (s);
  375. url_seek (h, -MAX_PAGE_SIZE, SEEK_END);
  376. while (!ogg_read_page (s, &i)){
  377. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
  378. idx = i;
  379. }
  380. if (idx != -1){
  381. s->streams[idx]->duration =
  382. ogg_gptopts (s, idx, ogg->streams[idx].granule);
  383. }
  384. ogg->size = url_filesize(h);
  385. ogg_restore (s, 0);
  386. return 0;
  387. }
  388. static int
  389. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  390. {
  391. ogg_t *ogg = s->priv_data;
  392. ogg->curidx = -1;
  393. //linear headers seek from start
  394. if (ogg_get_headers (s) < 0){
  395. return -1;
  396. }
  397. //linear granulepos seek from end
  398. ogg_get_length (s);
  399. //fill the extradata in the per codec callbacks
  400. return 0;
  401. }
  402. static int
  403. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  404. {
  405. ogg_t *ogg;
  406. ogg_stream_t *os;
  407. int idx = -1;
  408. //Get an ogg packet
  409. do{
  410. if (ogg_packet (s, &idx) < 0)
  411. return AVERROR_IO;
  412. }while (idx < 0 || !s->streams[idx]);
  413. ogg = s->priv_data;
  414. os = ogg->streams + idx;
  415. //Alloc a pkt
  416. if (av_new_packet (pkt, os->psize) < 0)
  417. return AVERROR_IO;
  418. pkt->stream_index = idx;
  419. memcpy (pkt->data, os->buf + os->pstart, os->psize);
  420. if (os->lastgp != -1LL){
  421. pkt->pts = ogg_gptopts (s, idx, os->lastgp);
  422. os->lastgp = -1;
  423. }
  424. //next
  425. os->pstart += os->psize;
  426. os->psize = 0;
  427. return os->psize;
  428. }
  429. static int
  430. ogg_read_close (AVFormatContext * s)
  431. {
  432. ogg_t *ogg = s->priv_data;
  433. int i;
  434. for (i = 0; i < ogg->nstreams; i++){
  435. av_free (ogg->streams[i].buf);
  436. av_free (ogg->streams[i].private);
  437. av_freep (&s->streams[i]->codec.extradata);
  438. }
  439. av_free (ogg->streams);
  440. return 0;
  441. }
  442. static int
  443. ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
  444. int flags)
  445. {
  446. ogg_t *ogg = s->priv_data;
  447. ByteIOContext *bc = &s->pb;
  448. uint64_t min = 0, max = ogg->size;
  449. uint64_t tmin = 0, tmax = s->duration;
  450. int64_t pts = AV_NOPTS_VALUE;
  451. ogg_save (s);
  452. while (min <= max){
  453. uint64_t p = min + (max - min) * (target_ts - tmin) / (tmax - tmin);
  454. int i = -1;
  455. url_fseek (bc, p, SEEK_SET);
  456. while (!ogg_read_page (s, &i)){
  457. if (ogg->streams[i].granule != 0 && ogg->streams[i].granule != -1)
  458. break;
  459. }
  460. if (i == -1)
  461. break;
  462. pts = ogg_gptopts (s, i, ogg->streams[i].granule);
  463. p = url_ftell (bc);
  464. if (ABS (pts - target_ts) < 1000000LL)
  465. break;
  466. if (pts > target_ts){
  467. max = p;
  468. tmax = pts;
  469. }else{
  470. min = p;
  471. tmin = pts;
  472. }
  473. }
  474. if (ABS (pts - target_ts) < 1000000LL){
  475. ogg_restore (s, 1);
  476. ogg_reset (ogg);
  477. }else{
  478. ogg_restore (s, 0);
  479. pts = AV_NOPTS_VALUE;
  480. }
  481. return pts;
  482. #if 0
  483. //later...
  484. int64_t pos;
  485. if (av_seek_frame_binary (s, stream_index, target_ts, flags) < 0)
  486. return -1;
  487. pos = url_ftell (&s->pb);
  488. ogg_read_timestamp (s, stream_index, &pos, pos - 1);
  489. #endif
  490. }
  491. #if 0
  492. static int64_t
  493. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  494. int64_t pos_limit)
  495. {
  496. ogg_t *ogg = s->priv_data;
  497. ByteIOContext *bc = &s->pb;
  498. int64_t pos, pts;
  499. if (*pos_arg < 0)
  500. return AV_NOPTS_VALUE;
  501. pos = *pos_arg;
  502. }
  503. #endif
  504. static AVInputFormat ogg_iformat = {
  505. "ogg",
  506. "Ogg",
  507. sizeof (ogg_t),
  508. NULL,
  509. ogg_read_header,
  510. ogg_read_packet,
  511. ogg_read_close,
  512. ogg_read_seek,
  513. // ogg_read_timestamp,
  514. .extensions = "ogg",
  515. };
  516. int
  517. ogg_init (void)
  518. {
  519. #if 0 // CONFIG_ENCODERS
  520. av_register_output_format (&ogg_oformat);
  521. #endif
  522. av_register_input_format (&ogg_iformat);
  523. return 0;
  524. }