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.

313 lines
9.3KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdint.h>
  19. #include "libavutil/common.h"
  20. #include "libavutil/intreadwrite.h"
  21. #include "libavutil/log.h"
  22. #include "libavutil/opt.h"
  23. #include "avcodec.h"
  24. #include "bsf.h"
  25. #include "h2645_parse.h"
  26. #include "h264.h"
  27. #include "hevc.h"
  28. #include "vc1_common.h"
  29. typedef struct ExtractExtradataContext {
  30. const AVClass *class;
  31. int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
  32. uint8_t **data, int *size);
  33. /* AVOptions */
  34. int remove;
  35. } ExtractExtradataContext;
  36. static int val_in_array(const int *arr, int len, int val)
  37. {
  38. int i;
  39. for (i = 0; i < len; i++)
  40. if (arr[i] == val)
  41. return 1;
  42. return 0;
  43. }
  44. static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
  45. uint8_t **data, int *size)
  46. {
  47. static const int extradata_nal_types_hevc[] = {
  48. HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,
  49. };
  50. static const int extradata_nal_types_h264[] = {
  51. H264_NAL_SPS, H264_NAL_PPS,
  52. };
  53. ExtractExtradataContext *s = ctx->priv_data;
  54. H2645Packet h2645_pkt = { 0 };
  55. int extradata_size = 0;
  56. const int *extradata_nal_types;
  57. int nb_extradata_nal_types;
  58. int i, has_sps = 0, has_vps = 0, ret = 0;
  59. if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
  60. extradata_nal_types = extradata_nal_types_hevc;
  61. nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);
  62. } else {
  63. extradata_nal_types = extradata_nal_types_h264;
  64. nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
  65. }
  66. ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size,
  67. ctx, 0, 0, ctx->par_in->codec_id);
  68. if (ret < 0)
  69. return ret;
  70. for (i = 0; i < h2645_pkt.nb_nals; i++) {
  71. H2645NAL *nal = &h2645_pkt.nals[i];
  72. if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
  73. extradata_size += nal->raw_size + 3;
  74. if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
  75. if (nal->type == HEVC_NAL_SPS) has_sps = 1;
  76. if (nal->type == HEVC_NAL_VPS) has_vps = 1;
  77. } else {
  78. if (nal->type == H264_NAL_SPS) has_sps = 1;
  79. }
  80. }
  81. }
  82. if (extradata_size &&
  83. ((ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||
  84. (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
  85. AVBufferRef *filtered_buf;
  86. uint8_t *extradata, *filtered_data;
  87. if (s->remove) {
  88. filtered_buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  89. if (!filtered_buf) {
  90. ret = AVERROR(ENOMEM);
  91. goto fail;
  92. }
  93. filtered_data = filtered_buf->data;
  94. }
  95. extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  96. if (!extradata) {
  97. av_buffer_unref(&filtered_buf);
  98. ret = AVERROR(ENOMEM);
  99. goto fail;
  100. }
  101. *data = extradata;
  102. *size = extradata_size;
  103. for (i = 0; i < h2645_pkt.nb_nals; i++) {
  104. H2645NAL *nal = &h2645_pkt.nals[i];
  105. if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
  106. nal->type)) {
  107. AV_WB24(extradata, 1); // startcode
  108. memcpy(extradata + 3, nal->raw_data, nal->raw_size);
  109. extradata += 3 + nal->raw_size;
  110. } else if (s->remove) {
  111. AV_WB24(filtered_data, 1); // startcode
  112. memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
  113. filtered_data += 3 + nal->raw_size;
  114. }
  115. }
  116. if (s->remove) {
  117. av_buffer_unref(&pkt->buf);
  118. pkt->buf = filtered_buf;
  119. pkt->data = filtered_buf->data;
  120. pkt->size = filtered_data - filtered_buf->data;
  121. }
  122. }
  123. fail:
  124. ff_h2645_packet_uninit(&h2645_pkt);
  125. return ret;
  126. }
  127. static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
  128. uint8_t **data, int *size)
  129. {
  130. ExtractExtradataContext *s = ctx->priv_data;
  131. uint32_t state = UINT32_MAX;
  132. int has_extradata = 0, extradata_size = 0;
  133. int i;
  134. for (i = 0; i < pkt->size; i++) {
  135. state = (state << 8) | pkt->data[i];
  136. if (IS_MARKER(state)) {
  137. if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
  138. has_extradata = 1;
  139. } else if (has_extradata) {
  140. extradata_size = i - 3;
  141. break;
  142. }
  143. }
  144. }
  145. if (extradata_size) {
  146. *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  147. if (!*data)
  148. return AVERROR(ENOMEM);
  149. memcpy(*data, pkt->data, extradata_size);
  150. *size = extradata_size;
  151. if (s->remove) {
  152. pkt->data += extradata_size;
  153. pkt->size -= extradata_size;
  154. }
  155. }
  156. return 0;
  157. }
  158. static int extract_extradata_mpeg124(AVBSFContext *ctx, AVPacket *pkt,
  159. uint8_t **data, int *size)
  160. {
  161. ExtractExtradataContext *s = ctx->priv_data;
  162. int is_mpeg12 = ctx->par_in->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  163. ctx->par_in->codec_id == AV_CODEC_ID_MPEG2VIDEO;
  164. uint32_t state = UINT32_MAX;
  165. int i;
  166. for (i = 0; i < pkt->size; i++) {
  167. state = (state << 8) | pkt->data[i];
  168. if ((is_mpeg12 && state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) ||
  169. (!is_mpeg12 && (state == 0x1B3 || state == 0x1B6))) {
  170. if (i > 3) {
  171. *size = i - 3;
  172. *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
  173. if (!*data)
  174. return AVERROR(ENOMEM);
  175. memcpy(*data, pkt->data, *size);
  176. if (s->remove) {
  177. pkt->data += *size;
  178. pkt->size -= *size;
  179. }
  180. }
  181. break;
  182. }
  183. }
  184. return 0;
  185. }
  186. static const struct {
  187. enum AVCodecID id;
  188. int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
  189. uint8_t **data, int *size);
  190. } extract_tab[] = {
  191. { AV_CODEC_ID_CAVS, extract_extradata_mpeg124 },
  192. { AV_CODEC_ID_H264, extract_extradata_h2645 },
  193. { AV_CODEC_ID_HEVC, extract_extradata_h2645 },
  194. { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg124 },
  195. { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg124 },
  196. { AV_CODEC_ID_MPEG4, extract_extradata_mpeg124 },
  197. { AV_CODEC_ID_VC1, extract_extradata_vc1 },
  198. };
  199. static int extract_extradata_init(AVBSFContext *ctx)
  200. {
  201. ExtractExtradataContext *s = ctx->priv_data;
  202. int i;
  203. for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
  204. if (extract_tab[i].id == ctx->par_in->codec_id) {
  205. s->extract = extract_tab[i].extract;
  206. break;
  207. }
  208. }
  209. if (!s->extract)
  210. return AVERROR_BUG;
  211. return 0;
  212. }
  213. static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *out)
  214. {
  215. ExtractExtradataContext *s = ctx->priv_data;
  216. AVPacket *in;
  217. uint8_t *extradata = NULL;
  218. int extradata_size;
  219. int ret = 0;
  220. ret = ff_bsf_get_packet(ctx, &in);
  221. if (ret < 0)
  222. return ret;
  223. ret = s->extract(ctx, in, &extradata, &extradata_size);
  224. if (ret < 0)
  225. goto fail;
  226. if (extradata) {
  227. ret = av_packet_add_side_data(in, AV_PKT_DATA_NEW_EXTRADATA,
  228. extradata, extradata_size);
  229. if (ret < 0) {
  230. av_freep(&extradata);
  231. goto fail;
  232. }
  233. }
  234. av_packet_move_ref(out, in);
  235. fail:
  236. av_packet_free(&in);
  237. return ret;
  238. }
  239. static const enum AVCodecID codec_ids[] = {
  240. AV_CODEC_ID_CAVS,
  241. AV_CODEC_ID_H264,
  242. AV_CODEC_ID_HEVC,
  243. AV_CODEC_ID_MPEG1VIDEO,
  244. AV_CODEC_ID_MPEG2VIDEO,
  245. AV_CODEC_ID_MPEG4,
  246. AV_CODEC_ID_VC1,
  247. AV_CODEC_ID_NONE,
  248. };
  249. #define OFFSET(x) offsetof(ExtractExtradataContext, x)
  250. static const AVOption options[] = {
  251. { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
  252. { .i64 = 0 }, 0, 1 },
  253. { NULL },
  254. };
  255. static const AVClass extract_extradata_class = {
  256. .class_name = "extract_extradata",
  257. .item_name = av_default_item_name,
  258. .option = options,
  259. .version = LIBAVUTIL_VERSION_INT,
  260. };
  261. const AVBitStreamFilter ff_extract_extradata_bsf = {
  262. .name = "extract_extradata",
  263. .codec_ids = codec_ids,
  264. .priv_data_size = sizeof(ExtractExtradataContext),
  265. .priv_class = &extract_extradata_class,
  266. .init = extract_extradata_init,
  267. .filter = extract_extradata_filter,
  268. };