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.

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