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.

569 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/log.h"
  20. #include "libavutil/mem.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/bprint.h"
  24. #include "avcodec.h"
  25. #include "bsf.h"
  26. struct AVBSFInternal {
  27. AVPacket *buffer_pkt;
  28. int eof;
  29. };
  30. void av_bsf_free(AVBSFContext **pctx)
  31. {
  32. AVBSFContext *ctx;
  33. if (!pctx || !*pctx)
  34. return;
  35. ctx = *pctx;
  36. if (ctx->filter->close)
  37. ctx->filter->close(ctx);
  38. if (ctx->filter->priv_class && ctx->priv_data)
  39. av_opt_free(ctx->priv_data);
  40. av_opt_free(ctx);
  41. if (ctx->internal)
  42. av_packet_free(&ctx->internal->buffer_pkt);
  43. av_freep(&ctx->internal);
  44. av_freep(&ctx->priv_data);
  45. avcodec_parameters_free(&ctx->par_in);
  46. avcodec_parameters_free(&ctx->par_out);
  47. av_freep(pctx);
  48. }
  49. static void *bsf_child_next(void *obj, void *prev)
  50. {
  51. AVBSFContext *ctx = obj;
  52. if (!prev && ctx->filter->priv_class)
  53. return ctx->priv_data;
  54. return NULL;
  55. }
  56. static const AVClass bsf_class = {
  57. .class_name = "AVBSFContext",
  58. .item_name = av_default_item_name,
  59. .version = LIBAVUTIL_VERSION_INT,
  60. .child_next = bsf_child_next,
  61. .child_class_next = ff_bsf_child_class_next,
  62. };
  63. const AVClass *av_bsf_get_class(void)
  64. {
  65. return &bsf_class;
  66. }
  67. int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
  68. {
  69. AVBSFContext *ctx;
  70. int ret;
  71. ctx = av_mallocz(sizeof(*ctx));
  72. if (!ctx)
  73. return AVERROR(ENOMEM);
  74. ctx->av_class = &bsf_class;
  75. ctx->filter = filter;
  76. ctx->par_in = avcodec_parameters_alloc();
  77. ctx->par_out = avcodec_parameters_alloc();
  78. if (!ctx->par_in || !ctx->par_out) {
  79. ret = AVERROR(ENOMEM);
  80. goto fail;
  81. }
  82. ctx->internal = av_mallocz(sizeof(*ctx->internal));
  83. if (!ctx->internal) {
  84. ret = AVERROR(ENOMEM);
  85. goto fail;
  86. }
  87. ctx->internal->buffer_pkt = av_packet_alloc();
  88. if (!ctx->internal->buffer_pkt) {
  89. ret = AVERROR(ENOMEM);
  90. goto fail;
  91. }
  92. av_opt_set_defaults(ctx);
  93. /* allocate priv data and init private options */
  94. if (filter->priv_data_size) {
  95. ctx->priv_data = av_mallocz(filter->priv_data_size);
  96. if (!ctx->priv_data) {
  97. ret = AVERROR(ENOMEM);
  98. goto fail;
  99. }
  100. if (filter->priv_class) {
  101. *(const AVClass **)ctx->priv_data = filter->priv_class;
  102. av_opt_set_defaults(ctx->priv_data);
  103. }
  104. }
  105. *pctx = ctx;
  106. return 0;
  107. fail:
  108. av_bsf_free(&ctx);
  109. return ret;
  110. }
  111. int av_bsf_init(AVBSFContext *ctx)
  112. {
  113. int ret, i;
  114. /* check that the codec is supported */
  115. if (ctx->filter->codec_ids) {
  116. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
  117. if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
  118. break;
  119. if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
  120. const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
  121. av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
  122. "bitstream filter '%s'. Supported codecs are: ",
  123. desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
  124. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
  125. desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
  126. av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
  127. desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
  128. }
  129. av_log(ctx, AV_LOG_ERROR, "\n");
  130. return AVERROR(EINVAL);
  131. }
  132. }
  133. /* initialize output parameters to be the same as input
  134. * init below might overwrite that */
  135. ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
  136. if (ret < 0)
  137. return ret;
  138. ctx->time_base_out = ctx->time_base_in;
  139. if (ctx->filter->init) {
  140. ret = ctx->filter->init(ctx);
  141. if (ret < 0)
  142. return ret;
  143. }
  144. return 0;
  145. }
  146. void av_bsf_flush(AVBSFContext *ctx)
  147. {
  148. ctx->internal->eof = 0;
  149. av_packet_unref(ctx->internal->buffer_pkt);
  150. if (ctx->filter->flush)
  151. ctx->filter->flush(ctx);
  152. }
  153. int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
  154. {
  155. int ret;
  156. if (!pkt || (!pkt->data && !pkt->side_data_elems)) {
  157. ctx->internal->eof = 1;
  158. return 0;
  159. }
  160. if (ctx->internal->eof) {
  161. av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
  162. return AVERROR(EINVAL);
  163. }
  164. if (ctx->internal->buffer_pkt->data ||
  165. ctx->internal->buffer_pkt->side_data_elems)
  166. return AVERROR(EAGAIN);
  167. ret = av_packet_make_refcounted(pkt);
  168. if (ret < 0)
  169. return ret;
  170. av_packet_move_ref(ctx->internal->buffer_pkt, pkt);
  171. return 0;
  172. }
  173. int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
  174. {
  175. return ctx->filter->filter(ctx, pkt);
  176. }
  177. int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
  178. {
  179. AVBSFInternal *in = ctx->internal;
  180. AVPacket *tmp_pkt;
  181. if (in->eof)
  182. return AVERROR_EOF;
  183. if (!ctx->internal->buffer_pkt->data &&
  184. !ctx->internal->buffer_pkt->side_data_elems)
  185. return AVERROR(EAGAIN);
  186. tmp_pkt = av_packet_alloc();
  187. if (!tmp_pkt)
  188. return AVERROR(ENOMEM);
  189. *pkt = ctx->internal->buffer_pkt;
  190. ctx->internal->buffer_pkt = tmp_pkt;
  191. return 0;
  192. }
  193. int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
  194. {
  195. AVBSFInternal *in = ctx->internal;
  196. if (in->eof)
  197. return AVERROR_EOF;
  198. if (!ctx->internal->buffer_pkt->data &&
  199. !ctx->internal->buffer_pkt->side_data_elems)
  200. return AVERROR(EAGAIN);
  201. av_packet_move_ref(pkt, ctx->internal->buffer_pkt);
  202. return 0;
  203. }
  204. typedef struct BSFListContext {
  205. const AVClass *class;
  206. AVBSFContext **bsfs;
  207. int nb_bsfs;
  208. unsigned idx; // index of currently processed BSF
  209. unsigned flushed_idx; // index of BSF being flushed
  210. char * item_name;
  211. } BSFListContext;
  212. static int bsf_list_init(AVBSFContext *bsf)
  213. {
  214. BSFListContext *lst = bsf->priv_data;
  215. int ret, i;
  216. const AVCodecParameters *cod_par = bsf->par_in;
  217. AVRational tb = bsf->time_base_in;
  218. for (i = 0; i < lst->nb_bsfs; ++i) {
  219. ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
  220. if (ret < 0)
  221. goto fail;
  222. lst->bsfs[i]->time_base_in = tb;
  223. ret = av_bsf_init(lst->bsfs[i]);
  224. if (ret < 0)
  225. goto fail;
  226. cod_par = lst->bsfs[i]->par_out;
  227. tb = lst->bsfs[i]->time_base_out;
  228. }
  229. bsf->time_base_out = tb;
  230. ret = avcodec_parameters_copy(bsf->par_out, cod_par);
  231. fail:
  232. return ret;
  233. }
  234. static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
  235. {
  236. BSFListContext *lst = bsf->priv_data;
  237. int ret;
  238. if (!lst->nb_bsfs)
  239. return ff_bsf_get_packet_ref(bsf, out);
  240. while (1) {
  241. if (lst->idx > lst->flushed_idx) {
  242. ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
  243. if (ret == AVERROR(EAGAIN)) {
  244. /* no more packets from idx-1, try with previous */
  245. ret = 0;
  246. lst->idx--;
  247. continue;
  248. } else if (ret == AVERROR_EOF) {
  249. /* filter idx-1 is done, continue with idx...nb_bsfs */
  250. lst->flushed_idx = lst->idx;
  251. continue;
  252. }else if (ret < 0) {
  253. /* filtering error */
  254. break;
  255. }
  256. } else {
  257. ret = ff_bsf_get_packet_ref(bsf, out);
  258. if (ret == AVERROR_EOF) {
  259. lst->idx = lst->flushed_idx;
  260. } else if (ret < 0)
  261. break;
  262. }
  263. if (lst->idx < lst->nb_bsfs) {
  264. AVPacket *pkt;
  265. if (ret == AVERROR_EOF && lst->idx == lst->flushed_idx) {
  266. /* ff_bsf_get_packet_ref returned EOF and idx is first
  267. * filter of yet not flushed filter chain */
  268. pkt = NULL;
  269. } else {
  270. pkt = out;
  271. }
  272. ret = av_bsf_send_packet(lst->bsfs[lst->idx], pkt);
  273. if (ret < 0)
  274. break;
  275. lst->idx++;
  276. } else {
  277. /* The end of filter chain, break to return result */
  278. break;
  279. }
  280. }
  281. if (ret < 0)
  282. av_packet_unref(out);
  283. return ret;
  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 = lst->flushed_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. int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
  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) {
  368. ret = av_opt_set_dict2(bsf, options, AV_OPT_SEARCH_CHILDREN);
  369. if (ret < 0)
  370. goto end;
  371. }
  372. ret = av_bsf_list_append(lst, bsf);
  373. end:
  374. if (ret < 0)
  375. av_bsf_free(&bsf);
  376. return ret;
  377. }
  378. int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
  379. {
  380. int ret = 0;
  381. BSFListContext *ctx;
  382. if ((*lst)->nb_bsfs == 1) {
  383. *bsf = (*lst)->bsfs[0];
  384. av_freep(&(*lst)->bsfs);
  385. (*lst)->nb_bsfs = 0;
  386. goto end;
  387. }
  388. ret = av_bsf_alloc(&ff_list_bsf, bsf);
  389. if (ret < 0)
  390. return ret;
  391. ctx = (*bsf)->priv_data;
  392. ctx->bsfs = (*lst)->bsfs;
  393. ctx->nb_bsfs = (*lst)->nb_bsfs;
  394. end:
  395. av_freep(lst);
  396. return ret;
  397. }
  398. static int bsf_parse_single(const char *str, AVBSFList *bsf_lst)
  399. {
  400. char *bsf_name, *bsf_options_str, *buf;
  401. AVDictionary *bsf_options = NULL;
  402. int ret = 0;
  403. if (!(buf = av_strdup(str)))
  404. return AVERROR(ENOMEM);
  405. bsf_name = av_strtok(buf, "=", &bsf_options_str);
  406. if (!bsf_name) {
  407. ret = AVERROR(EINVAL);
  408. goto end;
  409. }
  410. if (bsf_options_str) {
  411. ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0);
  412. if (ret < 0)
  413. goto end;
  414. }
  415. ret = av_bsf_list_append2(bsf_lst, bsf_name, &bsf_options);
  416. av_dict_free(&bsf_options);
  417. end:
  418. av_free(buf);
  419. return ret;
  420. }
  421. int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
  422. {
  423. AVBSFList *lst;
  424. char *bsf_str, *buf, *dup, *saveptr;
  425. int ret;
  426. if (!str)
  427. return av_bsf_get_null_filter(bsf_lst);
  428. lst = av_bsf_list_alloc();
  429. if (!lst)
  430. return AVERROR(ENOMEM);
  431. if (!(dup = buf = av_strdup(str))) {
  432. ret = AVERROR(ENOMEM);
  433. goto end;
  434. }
  435. while (1) {
  436. bsf_str = av_strtok(buf, ",", &saveptr);
  437. if (!bsf_str)
  438. break;
  439. ret = bsf_parse_single(bsf_str, lst);
  440. if (ret < 0)
  441. goto end;
  442. buf = NULL;
  443. }
  444. ret = av_bsf_list_finalize(&lst, bsf_lst);
  445. end:
  446. if (ret < 0)
  447. av_bsf_list_free(&lst);
  448. av_free(dup);
  449. return ret;
  450. }
  451. int av_bsf_get_null_filter(AVBSFContext **bsf)
  452. {
  453. return av_bsf_alloc(&ff_list_bsf, bsf);
  454. }