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.

561 lines
13KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <string.h>
  19. #include "libavutil/avassert.h"
  20. #include "libavutil/log.h"
  21. #include "libavutil/mem.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/bprint.h"
  25. #include "bsf.h"
  26. #include "bsf_internal.h"
  27. #include "codec_desc.h"
  28. #include "codec_par.h"
  29. #define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems)
  30. struct AVBSFInternal {
  31. AVPacket *buffer_pkt;
  32. int eof;
  33. };
  34. void av_bsf_free(AVBSFContext **pctx)
  35. {
  36. AVBSFContext *ctx;
  37. if (!pctx || !*pctx)
  38. return;
  39. ctx = *pctx;
  40. if (ctx->internal) {
  41. if (ctx->filter->close)
  42. ctx->filter->close(ctx);
  43. av_packet_free(&ctx->internal->buffer_pkt);
  44. av_freep(&ctx->internal);
  45. }
  46. if (ctx->filter->priv_class && ctx->priv_data)
  47. av_opt_free(ctx->priv_data);
  48. av_freep(&ctx->priv_data);
  49. avcodec_parameters_free(&ctx->par_in);
  50. avcodec_parameters_free(&ctx->par_out);
  51. av_freep(pctx);
  52. }
  53. static void *bsf_child_next(void *obj, void *prev)
  54. {
  55. AVBSFContext *ctx = obj;
  56. if (!prev && ctx->filter->priv_class)
  57. return ctx->priv_data;
  58. return NULL;
  59. }
  60. static const char *bsf_to_name(void *bsf)
  61. {
  62. return ((AVBSFContext *)bsf)->filter->name;
  63. }
  64. static const AVClass bsf_class = {
  65. .class_name = "AVBSFContext",
  66. .item_name = bsf_to_name,
  67. .version = LIBAVUTIL_VERSION_INT,
  68. .child_next = bsf_child_next,
  69. #if FF_API_CHILD_CLASS_NEXT
  70. .child_class_next = ff_bsf_child_class_next,
  71. #endif
  72. .child_class_iterate = ff_bsf_child_class_iterate,
  73. .category = AV_CLASS_CATEGORY_BITSTREAM_FILTER,
  74. };
  75. const AVClass *av_bsf_get_class(void)
  76. {
  77. return &bsf_class;
  78. }
  79. int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
  80. {
  81. AVBSFContext *ctx;
  82. AVBSFInternal *bsfi;
  83. int ret;
  84. ctx = av_mallocz(sizeof(*ctx));
  85. if (!ctx)
  86. return AVERROR(ENOMEM);
  87. ctx->av_class = &bsf_class;
  88. ctx->filter = filter;
  89. ctx->par_in = avcodec_parameters_alloc();
  90. ctx->par_out = avcodec_parameters_alloc();
  91. if (!ctx->par_in || !ctx->par_out) {
  92. ret = AVERROR(ENOMEM);
  93. goto fail;
  94. }
  95. /* allocate priv data and init private options */
  96. if (filter->priv_data_size) {
  97. ctx->priv_data = av_mallocz(filter->priv_data_size);
  98. if (!ctx->priv_data) {
  99. ret = AVERROR(ENOMEM);
  100. goto fail;
  101. }
  102. if (filter->priv_class) {
  103. *(const AVClass **)ctx->priv_data = filter->priv_class;
  104. av_opt_set_defaults(ctx->priv_data);
  105. }
  106. }
  107. /* Allocate AVBSFInternal; must happen after priv_data has been allocated
  108. * so that a filter->close needing priv_data is never called without. */
  109. bsfi = av_mallocz(sizeof(*bsfi));
  110. if (!bsfi) {
  111. ret = AVERROR(ENOMEM);
  112. goto fail;
  113. }
  114. ctx->internal = bsfi;
  115. bsfi->buffer_pkt = av_packet_alloc();
  116. if (!bsfi->buffer_pkt) {
  117. ret = AVERROR(ENOMEM);
  118. goto fail;
  119. }
  120. *pctx = ctx;
  121. return 0;
  122. fail:
  123. av_bsf_free(&ctx);
  124. return ret;
  125. }
  126. int av_bsf_init(AVBSFContext *ctx)
  127. {
  128. int ret, i;
  129. /* check that the codec is supported */
  130. if (ctx->filter->codec_ids) {
  131. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
  132. if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
  133. break;
  134. if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
  135. const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
  136. av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
  137. "bitstream filter '%s'. Supported codecs are: ",
  138. desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
  139. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
  140. enum AVCodecID codec_id = ctx->filter->codec_ids[i];
  141. av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
  142. avcodec_get_name(codec_id), codec_id);
  143. }
  144. av_log(ctx, AV_LOG_ERROR, "\n");
  145. return AVERROR(EINVAL);
  146. }
  147. }
  148. /* initialize output parameters to be the same as input
  149. * init below might overwrite that */
  150. ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
  151. if (ret < 0)
  152. return ret;
  153. ctx->time_base_out = ctx->time_base_in;
  154. if (ctx->filter->init) {
  155. ret = ctx->filter->init(ctx);
  156. if (ret < 0)
  157. return ret;
  158. }
  159. return 0;
  160. }
  161. void av_bsf_flush(AVBSFContext *ctx)
  162. {
  163. AVBSFInternal *bsfi = ctx->internal;
  164. bsfi->eof = 0;
  165. av_packet_unref(bsfi->buffer_pkt);
  166. if (ctx->filter->flush)
  167. ctx->filter->flush(ctx);
  168. }
  169. int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
  170. {
  171. AVBSFInternal *bsfi = ctx->internal;
  172. int ret;
  173. if (!pkt || IS_EMPTY(pkt)) {
  174. bsfi->eof = 1;
  175. return 0;
  176. }
  177. if (bsfi->eof) {
  178. av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
  179. return AVERROR(EINVAL);
  180. }
  181. if (!IS_EMPTY(bsfi->buffer_pkt))
  182. return AVERROR(EAGAIN);
  183. ret = av_packet_make_refcounted(pkt);
  184. if (ret < 0)
  185. return ret;
  186. av_packet_move_ref(bsfi->buffer_pkt, pkt);
  187. return 0;
  188. }
  189. int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
  190. {
  191. return ctx->filter->filter(ctx, pkt);
  192. }
  193. int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
  194. {
  195. AVBSFInternal *bsfi = ctx->internal;
  196. AVPacket *tmp_pkt;
  197. if (bsfi->eof)
  198. return AVERROR_EOF;
  199. if (IS_EMPTY(bsfi->buffer_pkt))
  200. return AVERROR(EAGAIN);
  201. tmp_pkt = av_packet_alloc();
  202. if (!tmp_pkt)
  203. return AVERROR(ENOMEM);
  204. *pkt = bsfi->buffer_pkt;
  205. bsfi->buffer_pkt = tmp_pkt;
  206. return 0;
  207. }
  208. int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
  209. {
  210. AVBSFInternal *bsfi = ctx->internal;
  211. if (bsfi->eof)
  212. return AVERROR_EOF;
  213. if (IS_EMPTY(bsfi->buffer_pkt))
  214. return AVERROR(EAGAIN);
  215. av_packet_move_ref(pkt, bsfi->buffer_pkt);
  216. return 0;
  217. }
  218. typedef struct BSFListContext {
  219. const AVClass *class;
  220. AVBSFContext **bsfs;
  221. int nb_bsfs;
  222. unsigned idx; // index of currently processed BSF
  223. char * item_name;
  224. } BSFListContext;
  225. static int bsf_list_init(AVBSFContext *bsf)
  226. {
  227. BSFListContext *lst = bsf->priv_data;
  228. int ret, i;
  229. const AVCodecParameters *cod_par = bsf->par_in;
  230. AVRational tb = bsf->time_base_in;
  231. for (i = 0; i < lst->nb_bsfs; ++i) {
  232. ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
  233. if (ret < 0)
  234. goto fail;
  235. lst->bsfs[i]->time_base_in = tb;
  236. ret = av_bsf_init(lst->bsfs[i]);
  237. if (ret < 0)
  238. goto fail;
  239. cod_par = lst->bsfs[i]->par_out;
  240. tb = lst->bsfs[i]->time_base_out;
  241. }
  242. bsf->time_base_out = tb;
  243. ret = avcodec_parameters_copy(bsf->par_out, cod_par);
  244. fail:
  245. return ret;
  246. }
  247. static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
  248. {
  249. BSFListContext *lst = bsf->priv_data;
  250. int ret, eof = 0;
  251. if (!lst->nb_bsfs)
  252. return ff_bsf_get_packet_ref(bsf, out);
  253. while (1) {
  254. /* get a packet from the previous filter up the chain */
  255. if (lst->idx)
  256. ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
  257. else
  258. ret = ff_bsf_get_packet_ref(bsf, out);
  259. if (ret == AVERROR(EAGAIN)) {
  260. if (!lst->idx)
  261. return ret;
  262. lst->idx--;
  263. continue;
  264. } else if (ret == AVERROR_EOF) {
  265. eof = 1;
  266. } else if (ret < 0)
  267. return ret;
  268. /* send it to the next filter down the chain */
  269. if (lst->idx < lst->nb_bsfs) {
  270. ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out);
  271. av_assert1(ret != AVERROR(EAGAIN));
  272. if (ret < 0) {
  273. av_packet_unref(out);
  274. return ret;
  275. }
  276. lst->idx++;
  277. eof = 0;
  278. } else if (eof) {
  279. return ret;
  280. } else {
  281. return 0;
  282. }
  283. }
  284. }
  285. static void bsf_list_flush(AVBSFContext *bsf)
  286. {
  287. BSFListContext *lst = bsf->priv_data;
  288. for (int i = 0; i < lst->nb_bsfs; i++)
  289. av_bsf_flush(lst->bsfs[i]);
  290. lst->idx = 0;
  291. }
  292. static void bsf_list_close(AVBSFContext *bsf)
  293. {
  294. BSFListContext *lst = bsf->priv_data;
  295. int i;
  296. for (i = 0; i < lst->nb_bsfs; ++i)
  297. av_bsf_free(&lst->bsfs[i]);
  298. av_freep(&lst->bsfs);
  299. av_freep(&lst->item_name);
  300. }
  301. static const char *bsf_list_item_name(void *ctx)
  302. {
  303. static const char *null_filter_name = "null";
  304. AVBSFContext *bsf_ctx = ctx;
  305. BSFListContext *lst = bsf_ctx->priv_data;
  306. if (!lst->nb_bsfs)
  307. return null_filter_name;
  308. if (!lst->item_name) {
  309. int i;
  310. AVBPrint bp;
  311. av_bprint_init(&bp, 16, 128);
  312. av_bprintf(&bp, "bsf_list(");
  313. for (i = 0; i < lst->nb_bsfs; i++)
  314. av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
  315. av_bprintf(&bp, ")");
  316. av_bprint_finalize(&bp, &lst->item_name);
  317. }
  318. return lst->item_name;
  319. }
  320. static const AVClass bsf_list_class = {
  321. .class_name = "bsf_list",
  322. .item_name = bsf_list_item_name,
  323. .version = LIBAVUTIL_VERSION_INT,
  324. };
  325. const AVBitStreamFilter ff_list_bsf = {
  326. .name = "bsf_list",
  327. .priv_data_size = sizeof(BSFListContext),
  328. .priv_class = &bsf_list_class,
  329. .init = bsf_list_init,
  330. .filter = bsf_list_filter,
  331. .flush = bsf_list_flush,
  332. .close = bsf_list_close,
  333. };
  334. struct AVBSFList {
  335. AVBSFContext **bsfs;
  336. int nb_bsfs;
  337. };
  338. AVBSFList *av_bsf_list_alloc(void)
  339. {
  340. return av_mallocz(sizeof(AVBSFList));
  341. }
  342. void av_bsf_list_free(AVBSFList **lst)
  343. {
  344. int i;
  345. if (!*lst)
  346. return;
  347. for (i = 0; i < (*lst)->nb_bsfs; ++i)
  348. av_bsf_free(&(*lst)->bsfs[i]);
  349. av_free((*lst)->bsfs);
  350. av_freep(lst);
  351. }
  352. int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
  353. {
  354. return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
  355. }
  356. static int bsf_list_append_internal(AVBSFList *lst, const char *bsf_name, const char *options, AVDictionary ** options_dict)
  357. {
  358. int ret;
  359. const AVBitStreamFilter *filter;
  360. AVBSFContext *bsf;
  361. filter = av_bsf_get_by_name(bsf_name);
  362. if (!filter)
  363. return AVERROR_BSF_NOT_FOUND;
  364. ret = av_bsf_alloc(filter, &bsf);
  365. if (ret < 0)
  366. return ret;
  367. if (options && filter->priv_class) {
  368. const AVOption *opt = av_opt_next(bsf->priv_data, NULL);
  369. const char * shorthand[2] = {NULL};
  370. if (opt)
  371. shorthand[0] = opt->name;
  372. ret = av_opt_set_from_string(bsf->priv_data, options, shorthand, "=", ":");
  373. if (ret < 0)
  374. goto end;
  375. }
  376. if (options_dict) {
  377. ret = av_opt_set_dict2(bsf, options_dict, AV_OPT_SEARCH_CHILDREN);
  378. if (ret < 0)
  379. goto end;
  380. }
  381. ret = av_bsf_list_append(lst, bsf);
  382. end:
  383. if (ret < 0)
  384. av_bsf_free(&bsf);
  385. return ret;
  386. }
  387. int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
  388. {
  389. return bsf_list_append_internal(lst, bsf_name, NULL, options);
  390. }
  391. int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
  392. {
  393. int ret = 0;
  394. BSFListContext *ctx;
  395. if ((*lst)->nb_bsfs == 1) {
  396. *bsf = (*lst)->bsfs[0];
  397. av_freep(&(*lst)->bsfs);
  398. (*lst)->nb_bsfs = 0;
  399. goto end;
  400. }
  401. ret = av_bsf_alloc(&ff_list_bsf, bsf);
  402. if (ret < 0)
  403. return ret;
  404. ctx = (*bsf)->priv_data;
  405. ctx->bsfs = (*lst)->bsfs;
  406. ctx->nb_bsfs = (*lst)->nb_bsfs;
  407. end:
  408. av_freep(lst);
  409. return ret;
  410. }
  411. static int bsf_parse_single(char *str, AVBSFList *bsf_lst)
  412. {
  413. char *bsf_name, *bsf_options_str;
  414. bsf_name = av_strtok(str, "=", &bsf_options_str);
  415. if (!bsf_name)
  416. return AVERROR(EINVAL);
  417. return bsf_list_append_internal(bsf_lst, bsf_name, bsf_options_str, NULL);
  418. }
  419. int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
  420. {
  421. AVBSFList *lst;
  422. char *bsf_str, *buf, *dup, *saveptr;
  423. int ret;
  424. if (!str)
  425. return av_bsf_get_null_filter(bsf_lst);
  426. lst = av_bsf_list_alloc();
  427. if (!lst)
  428. return AVERROR(ENOMEM);
  429. if (!(dup = buf = av_strdup(str))) {
  430. ret = AVERROR(ENOMEM);
  431. goto end;
  432. }
  433. while (bsf_str = av_strtok(buf, ",", &saveptr)) {
  434. ret = bsf_parse_single(bsf_str, lst);
  435. if (ret < 0)
  436. goto end;
  437. buf = NULL;
  438. }
  439. ret = av_bsf_list_finalize(&lst, bsf_lst);
  440. end:
  441. if (ret < 0)
  442. av_bsf_list_free(&lst);
  443. av_free(dup);
  444. return ret;
  445. }
  446. int av_bsf_get_null_filter(AVBSFContext **bsf)
  447. {
  448. return av_bsf_alloc(&ff_list_bsf, bsf);
  449. }