]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/bsf.c
avcodec/bsf: support shorthand options for av_bsf_list_parse_str
[ffmpeg] / libavcodec / bsf.c
index b9fc771a886a260e6494ee3b3e81b810a497bccc..85b63a5c9a8585abf68d563382475e95b08425d0 100644 (file)
@@ -28,6 +28,8 @@
 #include "avcodec.h"
 #include "bsf.h"
 
+#define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems)
+
 struct AVBSFInternal {
     AVPacket *buffer_pkt;
     int eof;
@@ -195,7 +197,7 @@ int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
     AVBSFInternal *bsfi = ctx->internal;
     int ret;
 
-    if (!pkt || (!pkt->data && !pkt->side_data_elems)) {
+    if (!pkt || IS_EMPTY(pkt)) {
         bsfi->eof = 1;
         return 0;
     }
@@ -205,8 +207,7 @@ int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
         return AVERROR(EINVAL);
     }
 
-    if (bsfi->buffer_pkt->data ||
-        bsfi->buffer_pkt->side_data_elems)
+    if (!IS_EMPTY(bsfi->buffer_pkt))
         return AVERROR(EAGAIN);
 
     ret = av_packet_make_refcounted(pkt);
@@ -230,8 +231,7 @@ int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
     if (bsfi->eof)
         return AVERROR_EOF;
 
-    if (!bsfi->buffer_pkt->data &&
-        !bsfi->buffer_pkt->side_data_elems)
+    if (IS_EMPTY(bsfi->buffer_pkt))
         return AVERROR(EAGAIN);
 
     tmp_pkt = av_packet_alloc();
@@ -251,8 +251,7 @@ int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
     if (bsfi->eof)
         return AVERROR_EOF;
 
-    if (!bsfi->buffer_pkt->data &&
-        !bsfi->buffer_pkt->side_data_elems)
+    if (IS_EMPTY(bsfi->buffer_pkt))
         return AVERROR(EAGAIN);
 
     av_packet_move_ref(pkt, bsfi->buffer_pkt);
@@ -432,7 +431,7 @@ int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
     return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
 }
 
-int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
+static int bsf_list_append_internal(AVBSFList *lst, const char *bsf_name, const char *options, AVDictionary ** options_dict)
 {
     int ret;
     const AVBitStreamFilter *filter;
@@ -446,8 +445,20 @@ int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** op
     if (ret < 0)
         return ret;
 
-    if (options) {
-        ret = av_opt_set_dict2(bsf, options, AV_OPT_SEARCH_CHILDREN);
+    if (options && filter->priv_class) {
+        const AVOption *opt = av_opt_next(bsf->priv_data, NULL);
+        const char * shorthand[2] = {NULL};
+
+        if (opt)
+            shorthand[0] = opt->name;
+
+        ret = av_opt_set_from_string(bsf->priv_data, options, shorthand, "=", ":");
+        if (ret < 0)
+            goto end;
+    }
+
+    if (options_dict) {
+        ret = av_opt_set_dict2(bsf, options_dict, AV_OPT_SEARCH_CHILDREN);
         if (ret < 0)
             goto end;
     }
@@ -461,6 +472,11 @@ end:
     return ret;
 }
 
+int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
+{
+    return bsf_list_append_internal(lst, bsf_name, NULL, options);
+}
+
 int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
 {
     int ret = 0;
@@ -487,33 +503,15 @@ end:
     return ret;
 }
 
-static int bsf_parse_single(const char *str, AVBSFList *bsf_lst)
+static int bsf_parse_single(char *str, AVBSFList *bsf_lst)
 {
-    char *bsf_name, *bsf_options_str, *buf;
-    AVDictionary *bsf_options = NULL;
-    int ret = 0;
+    char *bsf_name, *bsf_options_str;
 
-    if (!(buf = av_strdup(str)))
-        return AVERROR(ENOMEM);
-
-    bsf_name = av_strtok(buf, "=", &bsf_options_str);
-    if (!bsf_name) {
-        ret = AVERROR(EINVAL);
-        goto end;
-    }
-
-    if (bsf_options_str) {
-        ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0);
-        if (ret < 0)
-            goto end;
-    }
-
-    ret = av_bsf_list_append2(bsf_lst, bsf_name, &bsf_options);
+    bsf_name = av_strtok(str, "=", &bsf_options_str);
+    if (!bsf_name)
+        return AVERROR(EINVAL);
 
-end:
-    av_dict_free(&bsf_options);
-    av_free(buf);
-    return ret;
+    return bsf_list_append_internal(bsf_lst, bsf_name, bsf_options_str, NULL);
 }
 
 int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
@@ -534,11 +532,7 @@ int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
         goto end;
     }
 
-    while (1) {
-        bsf_str = av_strtok(buf, ",", &saveptr);
-        if (!bsf_str)
-            break;
-
+    while (bsf_str = av_strtok(buf, ",", &saveptr)) {
         ret = bsf_parse_single(bsf_str, lst);
         if (ret < 0)
             goto end;