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.

484 lines
13KB

  1. /*
  2. * AV1 Annex B demuxer
  3. * Copyright (c) 2019 James Almer <jamrial@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/fifo.h"
  24. #include "libavutil/opt.h"
  25. #include "libavcodec/av1_parse.h"
  26. #include "avformat.h"
  27. #include "avio_internal.h"
  28. #include "internal.h"
  29. //return < 0 if we need more data
  30. static int get_score(int type, int *seq)
  31. {
  32. switch (type) {
  33. case AV1_OBU_SEQUENCE_HEADER:
  34. *seq = 1;
  35. return -1;
  36. case AV1_OBU_FRAME:
  37. case AV1_OBU_FRAME_HEADER:
  38. return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
  39. case AV1_OBU_METADATA:
  40. case AV1_OBU_PADDING:
  41. return -1;
  42. default:
  43. break;
  44. }
  45. return 0;
  46. }
  47. static int read_header(AVFormatContext *s, const AVRational *framerate, AVBSFContext **bsf, void *logctx)
  48. {
  49. const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
  50. AVStream *st;
  51. int ret;
  52. if (!filter) {
  53. av_log(logctx, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
  54. "not found. This is a bug, please report it.\n");
  55. return AVERROR_BUG;
  56. }
  57. st = avformat_new_stream(s, NULL);
  58. if (!st)
  59. return AVERROR(ENOMEM);
  60. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  61. st->codecpar->codec_id = AV_CODEC_ID_AV1;
  62. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  63. st->internal->avctx->framerate = *framerate;
  64. // taken from rawvideo demuxers
  65. avpriv_set_pts_info(st, 64, 1, 1200000);
  66. ret = av_bsf_alloc(filter, bsf);
  67. if (ret < 0)
  68. return ret;
  69. ret = avcodec_parameters_copy((*bsf)->par_in, st->codecpar);
  70. if (ret < 0) {
  71. av_bsf_free(bsf);
  72. return ret;
  73. }
  74. ret = av_bsf_init(*bsf);
  75. if (ret < 0)
  76. av_bsf_free(bsf);
  77. return ret;
  78. }
  79. #define DEC AV_OPT_FLAG_DECODING_PARAM
  80. #if CONFIG_AV1_DEMUXER
  81. typedef struct AnnexBContext {
  82. const AVClass *class;
  83. AVBSFContext *bsf;
  84. uint32_t temporal_unit_size;
  85. uint32_t frame_unit_size;
  86. AVRational framerate;
  87. } AnnexBContext;
  88. static int leb(AVIOContext *pb, uint32_t *len) {
  89. int more, i = 0;
  90. uint8_t byte;
  91. *len = 0;
  92. do {
  93. unsigned bits;
  94. byte = avio_r8(pb);
  95. more = byte & 0x80;
  96. bits = byte & 0x7f;
  97. if (i <= 3 || (i == 4 && bits < (1 << 4)))
  98. *len |= bits << (i * 7);
  99. else if (bits)
  100. return AVERROR_INVALIDDATA;
  101. if (++i == 8 && more)
  102. return AVERROR_INVALIDDATA;
  103. if (pb->eof_reached || pb->error)
  104. return pb->error ? pb->error : AVERROR(EIO);
  105. } while (more);
  106. return i;
  107. }
  108. static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
  109. {
  110. int start_pos, temporal_id, spatial_id;
  111. int len;
  112. len = parse_obu_header(buf, size, obu_size, &start_pos,
  113. type, &temporal_id, &spatial_id);
  114. if (len < 0)
  115. return len;
  116. return 0;
  117. }
  118. static int annexb_probe(const AVProbeData *p)
  119. {
  120. AVIOContext pb;
  121. int64_t obu_size;
  122. uint32_t temporal_unit_size, frame_unit_size, obu_unit_size;
  123. int seq = 0;
  124. int ret, type, cnt = 0;
  125. ffio_init_context(&pb, p->buf, p->buf_size, 0,
  126. NULL, NULL, NULL, NULL);
  127. ret = leb(&pb, &temporal_unit_size);
  128. if (ret < 0)
  129. return 0;
  130. cnt += ret;
  131. ret = leb(&pb, &frame_unit_size);
  132. if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size)
  133. return 0;
  134. cnt += ret;
  135. ret = leb(&pb, &obu_unit_size);
  136. if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size)
  137. return 0;
  138. cnt += ret;
  139. frame_unit_size -= obu_unit_size + ret;
  140. avio_skip(&pb, obu_unit_size);
  141. if (pb.eof_reached || pb.error)
  142. return 0;
  143. // Check that the first OBU is a Temporal Delimiter.
  144. ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
  145. if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0)
  146. return 0;
  147. cnt += obu_unit_size;
  148. do {
  149. ret = leb(&pb, &obu_unit_size);
  150. if (ret < 0 || ((int64_t)obu_unit_size + ret) > frame_unit_size)
  151. return 0;
  152. cnt += ret;
  153. avio_skip(&pb, obu_unit_size);
  154. if (pb.eof_reached || pb.error)
  155. return 0;
  156. ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
  157. if (ret < 0)
  158. return 0;
  159. cnt += obu_unit_size;
  160. ret = get_score(type, &seq);
  161. if (ret >= 0)
  162. return ret;
  163. frame_unit_size -= obu_unit_size + ret;
  164. } while (frame_unit_size);
  165. return 0;
  166. }
  167. static int annexb_read_header(AVFormatContext *s)
  168. {
  169. AnnexBContext *c = s->priv_data;
  170. return read_header(s, &c->framerate, &c->bsf, c);
  171. }
  172. static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
  173. {
  174. AnnexBContext *c = s->priv_data;
  175. uint32_t obu_unit_size;
  176. int ret, len;
  177. retry:
  178. if (avio_feof(s->pb)) {
  179. if (c->temporal_unit_size || c->frame_unit_size)
  180. return AVERROR(EIO);
  181. goto end;
  182. }
  183. if (!c->temporal_unit_size) {
  184. len = leb(s->pb, &c->temporal_unit_size);
  185. if (len < 0) return AVERROR_INVALIDDATA;
  186. }
  187. if (!c->frame_unit_size) {
  188. len = leb(s->pb, &c->frame_unit_size);
  189. if (len < 0 || ((int64_t)c->frame_unit_size + len) > c->temporal_unit_size)
  190. return AVERROR_INVALIDDATA;
  191. c->temporal_unit_size -= len;
  192. }
  193. len = leb(s->pb, &obu_unit_size);
  194. if (len < 0 || ((int64_t)obu_unit_size + len) > c->frame_unit_size)
  195. return AVERROR_INVALIDDATA;
  196. ret = av_get_packet(s->pb, pkt, obu_unit_size);
  197. if (ret < 0)
  198. return ret;
  199. if (ret != obu_unit_size)
  200. return AVERROR(EIO);
  201. c->temporal_unit_size -= obu_unit_size + len;
  202. c->frame_unit_size -= obu_unit_size + len;
  203. end:
  204. ret = av_bsf_send_packet(c->bsf, pkt);
  205. if (ret < 0) {
  206. av_log(s, AV_LOG_ERROR, "Failed to send packet to "
  207. "av1_frame_merge filter\n");
  208. return ret;
  209. }
  210. ret = av_bsf_receive_packet(c->bsf, pkt);
  211. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  212. av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
  213. "send output packet\n");
  214. if (ret == AVERROR(EAGAIN))
  215. goto retry;
  216. return ret;
  217. }
  218. static int annexb_read_close(AVFormatContext *s)
  219. {
  220. AnnexBContext *c = s->priv_data;
  221. av_bsf_free(&c->bsf);
  222. return 0;
  223. }
  224. #define OFFSET(x) offsetof(AnnexBContext, x)
  225. static const AVOption annexb_options[] = {
  226. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
  227. { NULL },
  228. };
  229. #undef OFFSET
  230. static const AVClass annexb_demuxer_class = {
  231. .class_name = "AV1 Annex B demuxer",
  232. .item_name = av_default_item_name,
  233. .option = annexb_options,
  234. .version = LIBAVUTIL_VERSION_INT,
  235. };
  236. AVInputFormat ff_av1_demuxer = {
  237. .name = "av1",
  238. .long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
  239. .priv_data_size = sizeof(AnnexBContext),
  240. .read_probe = annexb_probe,
  241. .read_header = annexb_read_header,
  242. .read_packet = annexb_read_packet,
  243. .read_close = annexb_read_close,
  244. .extensions = "obu",
  245. .flags = AVFMT_GENERIC_INDEX,
  246. .priv_class = &annexb_demuxer_class,
  247. };
  248. #endif
  249. #if CONFIG_OBU_DEMUXER
  250. typedef struct ObuContext {
  251. const AVClass *class;
  252. AVBSFContext *bsf;
  253. AVRational framerate;
  254. AVFifoBuffer *fifo;
  255. } ObuContext;
  256. //For low overhead obu, we can't foresee the obu size before we parsed the header.
  257. //So, we can't use parse_obu_header here, since it will check size <= buf_size
  258. //see c27c7b49dc for more details
  259. static int read_obu_with_size(const uint8_t *buf, int buf_size, int64_t *obu_size, int *type)
  260. {
  261. GetBitContext gb;
  262. int ret, extension_flag, start_pos;
  263. int64_t size;
  264. ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
  265. if (ret < 0)
  266. return ret;
  267. if (get_bits1(&gb) != 0) // obu_forbidden_bit
  268. return AVERROR_INVALIDDATA;
  269. *type = get_bits(&gb, 4);
  270. extension_flag = get_bits1(&gb);
  271. if (!get_bits1(&gb)) // has_size_flag
  272. return AVERROR_INVALIDDATA;
  273. skip_bits1(&gb); // obu_reserved_1bit
  274. if (extension_flag) {
  275. get_bits(&gb, 3); // temporal_id
  276. get_bits(&gb, 2); // spatial_id
  277. skip_bits(&gb, 3); // extension_header_reserved_3bits
  278. }
  279. *obu_size = leb128(&gb);
  280. if (*obu_size > INT_MAX)
  281. return AVERROR_INVALIDDATA;
  282. if (get_bits_left(&gb) < 0)
  283. return AVERROR_INVALIDDATA;
  284. start_pos = get_bits_count(&gb) / 8;
  285. size = *obu_size + start_pos;
  286. if (size > INT_MAX)
  287. return AVERROR_INVALIDDATA;
  288. return size;
  289. }
  290. static int obu_probe(const AVProbeData *p)
  291. {
  292. int64_t obu_size;
  293. int seq = 0;
  294. int ret, type, cnt;
  295. // Check that the first OBU is a Temporal Delimiter.
  296. cnt = read_obu_with_size(p->buf, p->buf_size, &obu_size, &type);
  297. if (cnt < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size != 0)
  298. return 0;
  299. while (1) {
  300. ret = read_obu_with_size(p->buf + cnt, p->buf_size - cnt, &obu_size, &type);
  301. if (ret < 0 || obu_size <= 0)
  302. return 0;
  303. cnt += FFMIN(ret, p->buf_size - cnt);
  304. ret = get_score(type, &seq);
  305. if (ret >= 0)
  306. return ret;
  307. }
  308. return 0;
  309. }
  310. static int obu_read_header(AVFormatContext *s)
  311. {
  312. ObuContext *c = s->priv_data;
  313. c->fifo = av_fifo_alloc(MAX_OBU_HEADER_SIZE);
  314. if (!c->fifo)
  315. return AVERROR(ENOMEM);
  316. return read_header(s, &c->framerate, &c->bsf, c);
  317. }
  318. static int obu_get_packet(AVFormatContext *s, AVPacket *pkt)
  319. {
  320. ObuContext *c = s->priv_data;
  321. uint8_t header[MAX_OBU_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  322. int64_t obu_size;
  323. int size = av_fifo_space(c->fifo);
  324. int ret, len, type;
  325. av_fifo_generic_write(c->fifo, s->pb, size,
  326. (int (*)(void*, void*, int))avio_read);
  327. size = av_fifo_size(c->fifo);
  328. if (!size)
  329. return 0;
  330. av_fifo_generic_peek(c->fifo, header, size, NULL);
  331. len = read_obu_with_size(header, size, &obu_size, &type);
  332. if (len < 0) {
  333. av_log(c, AV_LOG_ERROR, "Failed to read obu\n");
  334. return len;
  335. }
  336. ret = av_new_packet(pkt, len);
  337. if (ret < 0) {
  338. av_log(c, AV_LOG_ERROR, "Failed to allocate packet for obu\n");
  339. return ret;
  340. }
  341. size = FFMIN(size, len);
  342. av_fifo_generic_read(c->fifo, pkt->data, size, NULL);
  343. len -= size;
  344. if (len > 0) {
  345. ret = avio_read(s->pb, pkt->data + size, len);
  346. if (ret != len) {
  347. av_log(c, AV_LOG_ERROR, "Failed to read %d frome file\n", len);
  348. return ret < 0 ? ret : AVERROR_INVALIDDATA;
  349. }
  350. }
  351. return 0;
  352. }
  353. static int obu_read_packet(AVFormatContext *s, AVPacket *pkt)
  354. {
  355. ObuContext *c = s->priv_data;
  356. int ret;
  357. while (1) {
  358. ret = obu_get_packet(s, pkt);
  359. if (ret < 0)
  360. return ret;
  361. ret = av_bsf_send_packet(c->bsf, pkt);
  362. if (ret < 0) {
  363. av_log(s, AV_LOG_ERROR, "Failed to send packet to "
  364. "av1_frame_merge filter\n");
  365. return ret;
  366. }
  367. ret = av_bsf_receive_packet(c->bsf, pkt);
  368. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  369. av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
  370. "send output packet\n");
  371. if (ret != AVERROR(EAGAIN))
  372. break;
  373. }
  374. return ret;
  375. }
  376. static int obu_read_close(AVFormatContext *s)
  377. {
  378. ObuContext *c = s->priv_data;
  379. av_fifo_freep(&c->fifo);
  380. av_bsf_free(&c->bsf);
  381. return 0;
  382. }
  383. #define OFFSET(x) offsetof(ObuContext, x)
  384. static const AVOption obu_options[] = {
  385. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
  386. { NULL },
  387. };
  388. #undef OFFSET
  389. static const AVClass obu_demuxer_class = {
  390. .class_name = "AV1 low overhead OBU demuxer",
  391. .item_name = av_default_item_name,
  392. .option = obu_options,
  393. .version = LIBAVUTIL_VERSION_INT,
  394. };
  395. AVInputFormat ff_obu_demuxer = {
  396. .name = "obu",
  397. .long_name = NULL_IF_CONFIG_SMALL("AV1 low overhead OBU"),
  398. .priv_data_size = sizeof(ObuContext),
  399. .read_probe = obu_probe,
  400. .read_header = obu_read_header,
  401. .read_packet = obu_read_packet,
  402. .read_close = obu_read_close,
  403. .extensions = "obu",
  404. .flags = AVFMT_GENERIC_INDEX,
  405. .priv_class = &obu_demuxer_class,
  406. };
  407. #endif