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.

247 lines
7.5KB

  1. /*
  2. * AV1 parser
  3. *
  4. * Copyright (C) 2018 James Almer <jamrial@gmail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "av1_parse.h"
  23. #include "cbs.h"
  24. #include "cbs_av1.h"
  25. #include "internal.h"
  26. #include "parser.h"
  27. typedef struct AV1ParseContext {
  28. CodedBitstreamContext *cbc;
  29. CodedBitstreamFragment temporal_unit;
  30. int parsed_extradata;
  31. } AV1ParseContext;
  32. static const enum AVPixelFormat pix_fmts_8bit[2][2] = {
  33. { AV_PIX_FMT_YUV444P, AV_PIX_FMT_NONE },
  34. { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P },
  35. };
  36. static const enum AVPixelFormat pix_fmts_10bit[2][2] = {
  37. { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_NONE },
  38. { AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10 },
  39. };
  40. static const enum AVPixelFormat pix_fmts_12bit[2][2] = {
  41. { AV_PIX_FMT_YUV444P12, AV_PIX_FMT_NONE },
  42. { AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12 },
  43. };
  44. static int av1_parser_parse(AVCodecParserContext *ctx,
  45. AVCodecContext *avctx,
  46. const uint8_t **out_data, int *out_size,
  47. const uint8_t *data, int size)
  48. {
  49. AV1ParseContext *s = ctx->priv_data;
  50. CodedBitstreamFragment *td = &s->temporal_unit;
  51. CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
  52. int ret;
  53. *out_data = data;
  54. *out_size = size;
  55. ctx->key_frame = -1;
  56. ctx->pict_type = AV_PICTURE_TYPE_NONE;
  57. ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  58. s->cbc->log_ctx = avctx;
  59. if (avctx->extradata_size && !s->parsed_extradata) {
  60. s->parsed_extradata = 1;
  61. ret = ff_cbs_read(s->cbc, td, avctx->extradata, avctx->extradata_size);
  62. if (ret < 0) {
  63. av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
  64. }
  65. ff_cbs_fragment_reset(s->cbc, td);
  66. }
  67. ret = ff_cbs_read(s->cbc, td, data, size);
  68. if (ret < 0) {
  69. av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
  70. goto end;
  71. }
  72. if (!av1->sequence_header) {
  73. av_log(avctx, AV_LOG_ERROR, "No sequence header available\n");
  74. goto end;
  75. }
  76. for (int i = 0; i < td->nb_units; i++) {
  77. CodedBitstreamUnit *unit = &td->units[i];
  78. AV1RawOBU *obu = unit->content;
  79. AV1RawSequenceHeader *seq = av1->sequence_header;
  80. AV1RawColorConfig *color = &seq->color_config;
  81. AV1RawFrameHeader *frame;
  82. int frame_type;
  83. if (unit->type == AV1_OBU_FRAME)
  84. frame = &obu->obu.frame.header;
  85. else if (unit->type == AV1_OBU_FRAME_HEADER)
  86. frame = &obu->obu.frame_header;
  87. else
  88. continue;
  89. if (obu->header.spatial_id > 0)
  90. continue;
  91. if (frame->show_existing_frame) {
  92. AV1ReferenceFrameState *ref = &av1->ref[frame->frame_to_show_map_idx];
  93. if (!ref->valid) {
  94. av_log(avctx, AV_LOG_ERROR, "Invalid reference frame\n");
  95. goto end;
  96. }
  97. ctx->width = ref->frame_width;
  98. ctx->height = ref->frame_height;
  99. frame_type = ref->frame_type;
  100. ctx->key_frame = 0;
  101. } else if (!frame->show_frame) {
  102. continue;
  103. } else {
  104. ctx->width = av1->frame_width;
  105. ctx->height = av1->frame_height;
  106. frame_type = frame->frame_type;
  107. ctx->key_frame = frame_type == AV1_FRAME_KEY;
  108. }
  109. avctx->profile = seq->seq_profile;
  110. avctx->level = seq->seq_level_idx[0];
  111. switch (frame_type) {
  112. case AV1_FRAME_KEY:
  113. case AV1_FRAME_INTRA_ONLY:
  114. ctx->pict_type = AV_PICTURE_TYPE_I;
  115. break;
  116. case AV1_FRAME_INTER:
  117. ctx->pict_type = AV_PICTURE_TYPE_P;
  118. break;
  119. case AV1_FRAME_SWITCH:
  120. ctx->pict_type = AV_PICTURE_TYPE_SP;
  121. break;
  122. }
  123. ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  124. switch (av1->bit_depth) {
  125. case 8:
  126. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
  127. : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
  128. break;
  129. case 10:
  130. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
  131. : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
  132. break;
  133. case 12:
  134. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
  135. : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
  136. break;
  137. }
  138. av_assert2(ctx->format != AV_PIX_FMT_NONE);
  139. avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients;
  140. avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries;
  141. avctx->color_trc = (enum AVColorTransferCharacteristic) color->transfer_characteristics;
  142. avctx->color_range = color->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  143. if (ctx->width != avctx->width || ctx->height != avctx->height) {
  144. ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
  145. if (ret < 0)
  146. goto end;
  147. }
  148. }
  149. if (avctx->framerate.num)
  150. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  151. end:
  152. ff_cbs_fragment_reset(s->cbc, td);
  153. s->cbc->log_ctx = NULL;
  154. return size;
  155. }
  156. static const CodedBitstreamUnitType decompose_unit_types[] = {
  157. AV1_OBU_TEMPORAL_DELIMITER,
  158. AV1_OBU_SEQUENCE_HEADER,
  159. AV1_OBU_FRAME_HEADER,
  160. AV1_OBU_TILE_GROUP,
  161. AV1_OBU_FRAME,
  162. };
  163. static av_cold int av1_parser_init(AVCodecParserContext *ctx)
  164. {
  165. AV1ParseContext *s = ctx->priv_data;
  166. int ret;
  167. ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
  168. if (ret < 0)
  169. return ret;
  170. s->cbc->decompose_unit_types = (CodedBitstreamUnitType *)decompose_unit_types;
  171. s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
  172. return 0;
  173. }
  174. static void av1_parser_close(AVCodecParserContext *ctx)
  175. {
  176. AV1ParseContext *s = ctx->priv_data;
  177. ff_cbs_fragment_free(s->cbc, &s->temporal_unit);
  178. ff_cbs_close(&s->cbc);
  179. }
  180. static int av1_parser_split(AVCodecContext *avctx,
  181. const uint8_t *buf, int buf_size)
  182. {
  183. AV1OBU obu;
  184. const uint8_t *ptr = buf, *end = buf + buf_size;
  185. while (ptr < end) {
  186. int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
  187. if (len < 0)
  188. break;
  189. if (obu.type == AV1_OBU_FRAME_HEADER ||
  190. obu.type == AV1_OBU_FRAME) {
  191. return ptr - buf;
  192. }
  193. ptr += len;
  194. buf_size -= len;
  195. }
  196. return 0;
  197. }
  198. AVCodecParser ff_av1_parser = {
  199. .codec_ids = { AV_CODEC_ID_AV1 },
  200. .priv_data_size = sizeof(AV1ParseContext),
  201. .parser_init = av1_parser_init,
  202. .parser_close = av1_parser_close,
  203. .parser_parse = av1_parser_parse,
  204. .split = av1_parser_split,
  205. };