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.

260 lines
7.9KB

  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(s->cbc, 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. int frame_type;
  88. if (unit->type == AV1_OBU_FRAME)
  89. frame = &obu->obu.frame.header;
  90. else if (unit->type == AV1_OBU_FRAME_HEADER)
  91. frame = &obu->obu.frame_header;
  92. else
  93. continue;
  94. if (obu->header.spatial_id > 0)
  95. continue;
  96. if (frame->show_existing_frame) {
  97. AV1ReferenceFrameState *ref = &av1->ref[frame->frame_to_show_map_idx];
  98. if (!ref->valid) {
  99. av_log(avctx, AV_LOG_ERROR, "Invalid reference frame\n");
  100. goto end;
  101. }
  102. ctx->width = ref->frame_width;
  103. ctx->height = ref->frame_height;
  104. frame_type = ref->frame_type;
  105. ctx->key_frame = 0;
  106. } else if (!frame->show_frame) {
  107. continue;
  108. } else {
  109. ctx->width = av1->frame_width;
  110. ctx->height = av1->frame_height;
  111. frame_type = frame->frame_type;
  112. ctx->key_frame = frame_type == AV1_FRAME_KEY;
  113. }
  114. switch (frame_type) {
  115. case AV1_FRAME_KEY:
  116. case AV1_FRAME_INTRA_ONLY:
  117. ctx->pict_type = AV_PICTURE_TYPE_I;
  118. break;
  119. case AV1_FRAME_INTER:
  120. ctx->pict_type = AV_PICTURE_TYPE_P;
  121. break;
  122. case AV1_FRAME_SWITCH:
  123. ctx->pict_type = AV_PICTURE_TYPE_SP;
  124. break;
  125. }
  126. ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  127. }
  128. switch (av1->bit_depth) {
  129. case 8:
  130. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
  131. : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
  132. break;
  133. case 10:
  134. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
  135. : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
  136. break;
  137. case 12:
  138. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
  139. : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
  140. break;
  141. }
  142. av_assert2(ctx->format != AV_PIX_FMT_NONE);
  143. if (!color->subsampling_x && !color->subsampling_y &&
  144. color->matrix_coefficients == AVCOL_SPC_RGB &&
  145. color->color_primaries == AVCOL_PRI_BT709 &&
  146. color->transfer_characteristics == AVCOL_TRC_IEC61966_2_1)
  147. ctx->format = pix_fmts_rgb[color->high_bitdepth + color->twelve_bit];
  148. avctx->profile = seq->seq_profile;
  149. avctx->level = seq->seq_level_idx[0];
  150. avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients;
  151. avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries;
  152. avctx->color_trc = (enum AVColorTransferCharacteristic) color->transfer_characteristics;
  153. avctx->color_range = color->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  154. if (ctx->width != avctx->width || ctx->height != avctx->height) {
  155. ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
  156. if (ret < 0)
  157. goto end;
  158. }
  159. if (avctx->framerate.num)
  160. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  161. end:
  162. ff_cbs_fragment_reset(s->cbc, td);
  163. s->cbc->log_ctx = NULL;
  164. return size;
  165. }
  166. static const CodedBitstreamUnitType decompose_unit_types[] = {
  167. AV1_OBU_TEMPORAL_DELIMITER,
  168. AV1_OBU_SEQUENCE_HEADER,
  169. AV1_OBU_FRAME_HEADER,
  170. AV1_OBU_TILE_GROUP,
  171. AV1_OBU_FRAME,
  172. };
  173. static av_cold int av1_parser_init(AVCodecParserContext *ctx)
  174. {
  175. AV1ParseContext *s = ctx->priv_data;
  176. int ret;
  177. ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
  178. if (ret < 0)
  179. return ret;
  180. s->cbc->decompose_unit_types = (CodedBitstreamUnitType *)decompose_unit_types;
  181. s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
  182. return 0;
  183. }
  184. static void av1_parser_close(AVCodecParserContext *ctx)
  185. {
  186. AV1ParseContext *s = ctx->priv_data;
  187. ff_cbs_fragment_free(s->cbc, &s->temporal_unit);
  188. ff_cbs_close(&s->cbc);
  189. }
  190. static int av1_parser_split(AVCodecContext *avctx,
  191. const uint8_t *buf, int buf_size)
  192. {
  193. AV1OBU obu;
  194. const uint8_t *ptr = buf, *end = buf + buf_size;
  195. while (ptr < end) {
  196. int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
  197. if (len < 0)
  198. break;
  199. if (obu.type == AV1_OBU_FRAME_HEADER ||
  200. obu.type == AV1_OBU_FRAME) {
  201. return ptr - buf;
  202. }
  203. ptr += len;
  204. buf_size -= len;
  205. }
  206. return 0;
  207. }
  208. AVCodecParser ff_av1_parser = {
  209. .codec_ids = { AV_CODEC_ID_AV1 },
  210. .priv_data_size = sizeof(AV1ParseContext),
  211. .parser_init = av1_parser_init,
  212. .parser_close = av1_parser_close,
  213. .parser_parse = av1_parser_parse,
  214. .split = av1_parser_split,
  215. };