新版本ffmpeg源碼簡單分析:Dictionary相關函數(shù)

我們先瀏覽一下Dictionary的定義蹭劈。

/**
 * @addtogroup lavu_dict AVDictionary
 * @ingroup lavu_data
 *
 * @brief Simple key:value store
 *
 * @{
 * Dictionaries are used for storing key:value pairs. To create
 * an AVDictionary, simply pass an address of a NULL pointer to
 * av_dict_set(). NULL can be used as an empty dictionary wherever
 * a pointer to an AVDictionary is required.
 * Use av_dict_get() to retrieve an entry or iterate over all
 * entries and finally av_dict_free() to free the dictionary
 * and all its contents.
 *
 @code
   AVDictionary *d = NULL;           // "create" an empty dictionary
   AVDictionaryEntry *t = NULL;

   av_dict_set(&d, "foo", "bar", 0); // add an entry

   char *k = av_strdup("key");       // if your strings are already allocated,
   char *v = av_strdup("value");     // you can avoid copying them like this
   av_dict_set(&d, k, v, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);

   while (t = av_dict_get(d, "", t, AV_DICT_IGNORE_SUFFIX)) {
       <....>                             // iterate over all entries in d
   }
   av_dict_free(&d);
 @endcode
 */

從這里我們可以看到Dictionary實際上就是強的鍵值映射,我們可以看一下AVDictionary結構體的定義行瑞。

// libavutil/dict.h
typedef struct AVDictionaryEntry {
    char *key;
    char *value;
} AVDictionaryEntry;
// libavutil/dict.c
struct AVDictionary {
    int count;
    AVDictionaryEntry *elems;
};

可以看出實際上AVDictionary實際上保存的就是key:value的點對關系嘹朗,還是用數(shù)組保存的,可能是為了節(jié)省空間诲侮,不過在搜索上可能還是有點差距镀虐。
然后就是這個結構體所包含的函數(shù):

/**
 * Get a dictionary entry with matching key.
 *
 * The returned entry key or value must not be changed, or it will
 * cause undefined behavior.
 *
 * To iterate through all the dictionary entries, you can set the matching key
 * to the null string "" and set the AV_DICT_IGNORE_SUFFIX flag.
 *
 * @param prev Set to the previous matching element to find the next.
 *             If set to NULL the first matching element is returned.
 * @param key matching key
 * @param flags a collection of AV_DICT_* flags controlling how the entry is retrieved
 * @return found entry or NULL in case no matching entry was found in the dictionary
 */
AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key,
                               const AVDictionaryEntry *prev, int flags);

/**
 * Get number of entries in dictionary.
 *
 * @param m dictionary
 * @return  number of entries in dictionary
 */
int av_dict_count(const AVDictionary *m);

/**
 * Set the given entry in *pm, overwriting an existing entry.
 *
 * Note: If AV_DICT_DONT_STRDUP_KEY or AV_DICT_DONT_STRDUP_VAL is set,
 * these arguments will be freed on error.
 *
 * Warning: Adding a new entry to a dictionary invalidates all existing entries
 * previously returned with av_dict_get.
 *
 * @param pm pointer to a pointer to a dictionary struct. If *pm is NULL
 * a dictionary struct is allocated and put in *pm.
 * @param key entry key to add to *pm (will either be av_strduped or added as a new key depending on flags)
 * @param value entry value to add to *pm (will be av_strduped or added as a new key depending on flags).
 *        Passing a NULL value will cause an existing entry to be deleted.
 * @return >= 0 on success otherwise an error code <0
 */
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags);

/**
 * Convenience wrapper for av_dict_set that converts the value to a string
 * and stores it.
 *
 * Note: If AV_DICT_DONT_STRDUP_KEY is set, key will be freed on error.
 */
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags);

/**
 * Parse the key/value pairs list and add the parsed entries to a dictionary.
 *
 * In case of failure, all the successfully set entries are stored in
 * *pm. You may need to manually free the created dictionary.
 *
 * @param key_val_sep  a 0-terminated list of characters used to separate
 *                     key from value
 * @param pairs_sep    a 0-terminated list of characters used to separate
 *                     two pairs from each other
 * @param flags        flags to use when adding to dictionary.
 *                     AV_DICT_DONT_STRDUP_KEY and AV_DICT_DONT_STRDUP_VAL
 *                     are ignored since the key/value tokens will always
 *                     be duplicated.
 * @return             0 on success, negative AVERROR code on failure
 */
int av_dict_parse_string(AVDictionary **pm, const char *str,
                         const char *key_val_sep, const char *pairs_sep,
                         int flags);

/**
 * Copy entries from one AVDictionary struct into another.
 * @param dst pointer to a pointer to a AVDictionary struct. If *dst is NULL,
 *            this function will allocate a struct for you and put it in *dst
 * @param src pointer to source AVDictionary struct
 * @param flags flags to use when setting entries in *dst
 * @note metadata is read using the AV_DICT_IGNORE_SUFFIX flag
 * @return 0 on success, negative AVERROR code on failure. If dst was allocated
 *           by this function, callers should free the associated memory.
 */
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags);

/**
 * Free all the memory allocated for an AVDictionary struct
 * and all keys and values.
 */
void av_dict_free(AVDictionary **m);

/**
 * Get dictionary entries as a string.
 *
 * Create a string containing dictionary's entries.
 * Such string may be passed back to av_dict_parse_string().
 * @note String is escaped with backslashes ('\').
 *
 * @param[in]  m             dictionary
 * @param[out] buffer        Pointer to buffer that will be allocated with string containg entries.
 *                           Buffer must be freed by the caller when is no longer needed.
 * @param[in]  key_val_sep   character used to separate key from value
 * @param[in]  pairs_sep     character used to separate two pairs from each other
 * @return                   >= 0 on success, negative on error
 * @warning Separators cannot be neither '\\' nor '\0'. They also cannot be the same.
 */
int av_dict_get_string(const AVDictionary *m, char **buffer,
                       const char key_val_sep, const char pairs_sep);

里面都解釋的很清楚,就不做太多的解釋沟绪。
由于av_dict_set用的比較多刮便,就詳細說一下。

參數(shù)介紹:
  pm: 簡單地說就是你要設置的dictionary的指針绽慈。
  key: key:value映射的鍵恨旱。
  value: key:value映射的值。
  flag: 設置key和value是否不重新申請內存坝疼,直接使用傳入的指針搜贤。比如AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL,就表示傳入的指針不申請內存裙士,直接使用入客。

之后就沒什么了。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市桌硫,隨后出現(xiàn)的幾起案子夭咬,更是在濱河造成了極大的恐慌,老刑警劉巖铆隘,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件卓舵,死亡現(xiàn)場離奇詭異,居然都是意外死亡膀钠,警方通過查閱死者的電腦和手機掏湾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來肿嘲,“玉大人融击,你說我怎么就攤上這事■撸” “怎么了尊浪?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長封救。 經常有香客問我拇涤,道長,這世上最難降的妖魔是什么誉结? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任鹅士,我火速辦了婚禮,結果婚禮上惩坑,老公的妹妹穿的比我還像新娘掉盅。我一直安慰自己,他們只是感情好旭贬,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布怔接。 她就那樣靜靜地躺著,像睡著了一般稀轨。 火紅的嫁衣襯著肌膚如雪扼脐。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天奋刽,我揣著相機與錄音瓦侮,去河邊找鬼。 笑死佣谐,一個胖子當著我的面吹牛肚吏,可吹牛的內容都是我干的。 我是一名探鬼主播狭魂,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼罚攀,長吁一口氣:“原來是場噩夢啊……” “哼党觅!你這毒婦竟也來了?” 一聲冷哼從身側響起斋泄,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤杯瞻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后炫掐,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體魁莉,經...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年募胃,在試婚紗的時候發(fā)現(xiàn)自己被綠了旗唁。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡痹束,死狀恐怖检疫,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情祷嘶,我是刑警寧澤电谣,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站抹蚀,受9級特大地震影響,放射性物質發(fā)生泄漏企垦。R本人自食惡果不足惜环壤,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望钞诡。 院中可真熱鬧郑现,春花似錦、人聲如沸荧降。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽朵诫。三九已至辛友,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間剪返,已是汗流浹背废累。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留脱盲,地道東北人邑滨。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像钱反,于是被迫代替她去往敵國和親掖看。 傳聞我的和親對象是個殘疾皇子匣距,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內容

  • 轉載自:https://halfrost.com/go_map_chapter_one/ https://half...
    HuJay閱讀 6,147評論 1 5
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,100評論 1 32
  • 轉至元數(shù)據(jù)結尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 1,715評論 0 9
  • 個人筆記哎壳,方便自己查閱使用 Py.LangSpec.Contents Refs Built-in Closure ...
    freenik閱讀 67,703評論 0 5
  • avoid avoid doing sth It seems you neet to apologize quic...
    來一起唄閱讀 207評論 0 0