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.

246 lines
7.4KB

  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 const enum AVPixelFormat pix_fmts_rgb[3] = {
  45. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
  46. };
  47. static int av1_parser_parse(AVCodecParserContext *ctx,
  48. AVCodecContext *avctx,
  49. const uint8_t **out_data, int *out_size,
  50. const uint8_t *data, int size)
  51. {
  52. AV1ParseContext *s = ctx->priv_data;
  53. CodedBitstreamFragment *td = &s->temporal_unit;
  54. CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
  55. AV1RawSequenceHeader *seq;
  56. AV1RawColorConfig *color;
  57. int ret;
  58. *out_data = data;
  59. *out_size = size;
  60. ctx->key_frame = -1;
  61. ctx->pict_type = AV_PICTURE_TYPE_NONE;
  62. ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  63. s->cbc->log_ctx = avctx;
  64. if (avctx->extradata_size && !s->parsed_extradata) {
  65. s->parsed_extradata = 1;
  66. ret = ff_cbs_read(s->cbc, td, avctx->extradata, avctx->extradata_size);
  67. if (ret < 0) {
  68. av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
  69. }
  70. ff_cbs_fragment_reset(td);
  71. }
  72. ret = ff_cbs_read(s->cbc, td, data, size);
  73. if (ret < 0) {
  74. av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
  75. goto end;
  76. }
  77. if (!av1->sequence_header) {
  78. av_log(avctx, AV_LOG_ERROR, "No sequence header available\n");
  79. goto end;
  80. }
  81. seq = av1->sequence_header;
  82. color = &seq->color_config;
  83. for (int i = 0; i < td->nb_units; i++) {
  84. CodedBitstreamUnit *unit = &td->units[i];
  85. AV1RawOBU *obu = unit->content;
  86. AV1RawFrameHeader *frame;
  87. if (unit->type == AV1_OBU_FRAME)
  88. frame = &obu->obu.frame.header;
  89. else if (unit->type == AV1_OBU_FRAME_HEADER)
  90. frame = &obu->obu.frame_header;
  91. else
  92. continue;
  93. if (obu->header.spatial_id > 0)
  94. continue;
  95. if (!frame->show_frame)
  96. continue;
  97. ctx->width = frame->frame_width_minus_1 + 1;
  98. ctx->height = frame->frame_height_minus_1 + 1;
  99. ctx->key_frame = frame->frame_type == AV1_FRAME_KEY && !frame->show_existing_frame;
  100. switch (frame->frame_type) {
  101. case AV1_FRAME_KEY:
  102. case AV1_FRAME_INTRA_ONLY:
  103. ctx->pict_type = AV_PICTURE_TYPE_I;
  104. break;
  105. case AV1_FRAME_INTER:
  106. ctx->pict_type = AV_PICTURE_TYPE_P;
  107. break;
  108. case AV1_FRAME_SWITCH:
  109. ctx->pict_type = AV_PICTURE_TYPE_SP;
  110. break;
  111. }
  112. ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  113. }
  114. switch (av1->bit_depth) {
  115. case 8:
  116. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
  117. : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
  118. break;
  119. case 10:
  120. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
  121. : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
  122. break;
  123. case 12:
  124. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
  125. : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
  126. break;
  127. }
  128. av_assert2(ctx->format != AV_PIX_FMT_NONE);
  129. if (!color->subsampling_x && !color->subsampling_y &&
  130. color->matrix_coefficients == AVCOL_SPC_RGB &&
  131. color->color_primaries == AVCOL_PRI_BT709 &&
  132. color->transfer_characteristics == AVCOL_TRC_IEC61966_2_1)
  133. ctx->format = pix_fmts_rgb[color->high_bitdepth + color->twelve_bit];
  134. avctx->pix_fmt = ctx->format;
  135. avctx->profile = seq->seq_profile;
  136. avctx->level = seq->seq_level_idx[0];
  137. avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients;
  138. avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries;
  139. avctx->color_trc = (enum AVColorTransferCharacteristic) color->transfer_characteristics;
  140. avctx->color_range = color->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  141. if (ctx->width != avctx->width || ctx->height != avctx->height) {
  142. ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
  143. if (ret < 0)
  144. goto end;
  145. }
  146. if (avctx->framerate.num)
  147. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  148. end:
  149. ff_cbs_fragment_reset(td);
  150. s->cbc->log_ctx = NULL;
  151. return size;
  152. }
  153. static const CodedBitstreamUnitType decompose_unit_types[] = {
  154. AV1_OBU_TEMPORAL_DELIMITER,
  155. AV1_OBU_SEQUENCE_HEADER,
  156. AV1_OBU_FRAME_HEADER,
  157. AV1_OBU_TILE_GROUP,
  158. AV1_OBU_FRAME,
  159. };
  160. static av_cold int av1_parser_init(AVCodecParserContext *ctx)
  161. {
  162. AV1ParseContext *s = ctx->priv_data;
  163. int ret;
  164. ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
  165. if (ret < 0)
  166. return ret;
  167. s->cbc->decompose_unit_types = (CodedBitstreamUnitType *)decompose_unit_types;
  168. s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
  169. return 0;
  170. }
  171. static void av1_parser_close(AVCodecParserContext *ctx)
  172. {
  173. AV1ParseContext *s = ctx->priv_data;
  174. ff_cbs_fragment_free(&s->temporal_unit);
  175. ff_cbs_close(&s->cbc);
  176. }
  177. static int av1_parser_split(AVCodecContext *avctx,
  178. const uint8_t *buf, int buf_size)
  179. {
  180. AV1OBU obu;
  181. const uint8_t *ptr = buf, *end = buf + buf_size;
  182. while (ptr < end) {
  183. int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
  184. if (len < 0)
  185. break;
  186. if (obu.type == AV1_OBU_FRAME_HEADER ||
  187. obu.type == AV1_OBU_FRAME) {
  188. return ptr - buf;
  189. }
  190. ptr += len;
  191. buf_size -= len;
  192. }
  193. return 0;
  194. }
  195. AVCodecParser ff_av1_parser = {
  196. .codec_ids = { AV_CODEC_ID_AV1 },
  197. .priv_data_size = sizeof(AV1ParseContext),
  198. .parser_init = av1_parser_init,
  199. .parser_close = av1_parser_close,
  200. .parser_parse = av1_parser_parse,
  201. .split = av1_parser_split,
  202. };