frame.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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 "channel_layout.h"
  19. #include "avassert.h"
  20. #include "buffer.h"
  21. #include "common.h"
  22. #include "dict.h"
  23. #include "frame.h"
  24. #include "imgutils.h"
  25. #include "mem.h"
  26. #include "samplefmt.h"
  27. #if FF_API_FRAME_GET_SET
  28. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  29. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  30. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  31. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  32. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  33. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  34. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  35. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  36. MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
  37. MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
  38. MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
  39. #endif
  40. #define CHECK_CHANNELS_CONSISTENCY(frame) \
  41. av_assert2(!(frame)->channel_layout || \
  42. (frame)->channels == \
  43. av_get_channel_layout_nb_channels((frame)->channel_layout))
  44. #if FF_API_FRAME_QP
  45. struct qp_properties {
  46. int stride;
  47. int type;
  48. };
  49. int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
  50. {
  51. struct qp_properties *p;
  52. AVFrameSideData *sd;
  53. AVBufferRef *ref;
  54. FF_DISABLE_DEPRECATION_WARNINGS
  55. av_buffer_unref(&f->qp_table_buf);
  56. f->qp_table_buf = buf;
  57. f->qscale_table = buf->data;
  58. f->qstride = stride;
  59. f->qscale_type = qp_type;
  60. FF_ENABLE_DEPRECATION_WARNINGS
  61. av_frame_remove_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES);
  62. av_frame_remove_side_data(f, AV_FRAME_DATA_QP_TABLE_DATA);
  63. ref = av_buffer_ref(buf);
  64. if (!av_frame_new_side_data_from_buf(f, AV_FRAME_DATA_QP_TABLE_DATA, ref)) {
  65. av_buffer_unref(&ref);
  66. return AVERROR(ENOMEM);
  67. }
  68. sd = av_frame_new_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES,
  69. sizeof(struct qp_properties));
  70. if (!sd)
  71. return AVERROR(ENOMEM);
  72. p = (struct qp_properties *)sd->data;
  73. p->stride = stride;
  74. p->type = qp_type;
  75. return 0;
  76. }
  77. int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
  78. {
  79. AVBufferRef *buf = NULL;
  80. *stride = 0;
  81. *type = 0;
  82. FF_DISABLE_DEPRECATION_WARNINGS
  83. if (f->qp_table_buf) {
  84. *stride = f->qstride;
  85. *type = f->qscale_type;
  86. buf = f->qp_table_buf;
  87. FF_ENABLE_DEPRECATION_WARNINGS
  88. } else {
  89. AVFrameSideData *sd;
  90. struct qp_properties *p;
  91. sd = av_frame_get_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES);
  92. if (!sd)
  93. return NULL;
  94. p = (struct qp_properties *)sd->data;
  95. sd = av_frame_get_side_data(f, AV_FRAME_DATA_QP_TABLE_DATA);
  96. if (!sd)
  97. return NULL;
  98. *stride = p->stride;
  99. *type = p->type;
  100. buf = sd->buf;
  101. }
  102. return buf ? buf->data : NULL;
  103. }
  104. #endif
  105. const char *av_get_colorspace_name(enum AVColorSpace val)
  106. {
  107. static const char * const name[] = {
  108. [AVCOL_SPC_RGB] = "GBR",
  109. [AVCOL_SPC_BT709] = "bt709",
  110. [AVCOL_SPC_FCC] = "fcc",
  111. [AVCOL_SPC_BT470BG] = "bt470bg",
  112. [AVCOL_SPC_SMPTE170M] = "smpte170m",
  113. [AVCOL_SPC_SMPTE240M] = "smpte240m",
  114. [AVCOL_SPC_YCOCG] = "YCgCo",
  115. };
  116. if ((unsigned)val >= FF_ARRAY_ELEMS(name))
  117. return NULL;
  118. return name[val];
  119. }
  120. static void get_frame_defaults(AVFrame *frame)
  121. {
  122. if (frame->extended_data != frame->data)
  123. av_freep(&frame->extended_data);
  124. memset(frame, 0, sizeof(*frame));
  125. frame->pts =
  126. frame->pkt_dts = AV_NOPTS_VALUE;
  127. #if FF_API_PKT_PTS
  128. FF_DISABLE_DEPRECATION_WARNINGS
  129. frame->pkt_pts = AV_NOPTS_VALUE;
  130. FF_ENABLE_DEPRECATION_WARNINGS
  131. #endif
  132. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  133. frame->pkt_duration = 0;
  134. frame->pkt_pos = -1;
  135. frame->pkt_size = -1;
  136. frame->key_frame = 1;
  137. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  138. frame->format = -1; /* unknown */
  139. frame->extended_data = frame->data;
  140. frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
  141. frame->color_trc = AVCOL_TRC_UNSPECIFIED;
  142. frame->colorspace = AVCOL_SPC_UNSPECIFIED;
  143. frame->color_range = AVCOL_RANGE_UNSPECIFIED;
  144. frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  145. frame->flags = 0;
  146. }
  147. static void free_side_data(AVFrameSideData **ptr_sd)
  148. {
  149. AVFrameSideData *sd = *ptr_sd;
  150. av_buffer_unref(&sd->buf);
  151. av_dict_free(&sd->metadata);
  152. av_freep(ptr_sd);
  153. }
  154. static void wipe_side_data(AVFrame *frame)
  155. {
  156. int i;
  157. for (i = 0; i < frame->nb_side_data; i++) {
  158. free_side_data(&frame->side_data[i]);
  159. }
  160. frame->nb_side_data = 0;
  161. av_freep(&frame->side_data);
  162. }
  163. AVFrame *av_frame_alloc(void)
  164. {
  165. AVFrame *frame = av_mallocz(sizeof(*frame));
  166. if (!frame)
  167. return NULL;
  168. frame->extended_data = NULL;
  169. get_frame_defaults(frame);
  170. return frame;
  171. }
  172. void av_frame_free(AVFrame **frame)
  173. {
  174. if (!frame || !*frame)
  175. return;
  176. av_frame_unref(*frame);
  177. av_freep(frame);
  178. }
  179. static int get_video_buffer(AVFrame *frame, int align)
  180. {
  181. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  182. int ret, i;
  183. if (!desc)
  184. return AVERROR(EINVAL);
  185. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  186. return ret;
  187. if (!frame->linesize[0]) {
  188. if (align <= 0)
  189. align = 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */
  190. for(i=1; i<=align; i+=i) {
  191. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  192. FFALIGN(frame->width, i));
  193. if (ret < 0)
  194. return ret;
  195. if (!(frame->linesize[0] & (align-1)))
  196. break;
  197. }
  198. for (i = 0; i < 4 && frame->linesize[i]; i++)
  199. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  200. }
  201. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  202. int h = FFALIGN(frame->height, 32);
  203. if (i == 1 || i == 2)
  204. h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
  205. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
  206. if (!frame->buf[i])
  207. goto fail;
  208. frame->data[i] = frame->buf[i]->data;
  209. }
  210. if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & FF_PSEUDOPAL) {
  211. av_buffer_unref(&frame->buf[1]);
  212. frame->buf[1] = av_buffer_alloc(AVPALETTE_SIZE);
  213. if (!frame->buf[1])
  214. goto fail;
  215. frame->data[1] = frame->buf[1]->data;
  216. }
  217. frame->extended_data = frame->data;
  218. return 0;
  219. fail:
  220. av_frame_unref(frame);
  221. return AVERROR(ENOMEM);
  222. }
  223. static int get_audio_buffer(AVFrame *frame, int align)
  224. {
  225. int channels;
  226. int planar = av_sample_fmt_is_planar(frame->format);
  227. int planes;
  228. int ret, i;
  229. if (!frame->channels)
  230. frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  231. channels = frame->channels;
  232. planes = planar ? channels : 1;
  233. CHECK_CHANNELS_CONSISTENCY(frame);
  234. if (!frame->linesize[0]) {
  235. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  236. frame->nb_samples, frame->format,
  237. align);
  238. if (ret < 0)
  239. return ret;
  240. }
  241. if (planes > AV_NUM_DATA_POINTERS) {
  242. frame->extended_data = av_mallocz_array(planes,
  243. sizeof(*frame->extended_data));
  244. frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
  245. sizeof(*frame->extended_buf));
  246. if (!frame->extended_data || !frame->extended_buf) {
  247. av_freep(&frame->extended_data);
  248. av_freep(&frame->extended_buf);
  249. return AVERROR(ENOMEM);
  250. }
  251. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  252. } else
  253. frame->extended_data = frame->data;
  254. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  255. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  256. if (!frame->buf[i]) {
  257. av_frame_unref(frame);
  258. return AVERROR(ENOMEM);
  259. }
  260. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  261. }
  262. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  263. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  264. if (!frame->extended_buf[i]) {
  265. av_frame_unref(frame);
  266. return AVERROR(ENOMEM);
  267. }
  268. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  269. }
  270. return 0;
  271. }
  272. int av_frame_get_buffer(AVFrame *frame, int align)
  273. {
  274. if (frame->format < 0)
  275. return AVERROR(EINVAL);
  276. if (frame->width > 0 && frame->height > 0)
  277. return get_video_buffer(frame, align);
  278. else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
  279. return get_audio_buffer(frame, align);
  280. return AVERROR(EINVAL);
  281. }
  282. static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
  283. {
  284. int i;
  285. dst->key_frame = src->key_frame;
  286. dst->pict_type = src->pict_type;
  287. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  288. dst->crop_top = src->crop_top;
  289. dst->crop_bottom = src->crop_bottom;
  290. dst->crop_left = src->crop_left;
  291. dst->crop_right = src->crop_right;
  292. dst->pts = src->pts;
  293. dst->repeat_pict = src->repeat_pict;
  294. dst->interlaced_frame = src->interlaced_frame;
  295. dst->top_field_first = src->top_field_first;
  296. dst->palette_has_changed = src->palette_has_changed;
  297. dst->sample_rate = src->sample_rate;
  298. dst->opaque = src->opaque;
  299. #if FF_API_PKT_PTS
  300. FF_DISABLE_DEPRECATION_WARNINGS
  301. dst->pkt_pts = src->pkt_pts;
  302. FF_ENABLE_DEPRECATION_WARNINGS
  303. #endif
  304. dst->pkt_dts = src->pkt_dts;
  305. dst->pkt_pos = src->pkt_pos;
  306. dst->pkt_size = src->pkt_size;
  307. dst->pkt_duration = src->pkt_duration;
  308. dst->reordered_opaque = src->reordered_opaque;
  309. dst->quality = src->quality;
  310. dst->best_effort_timestamp = src->best_effort_timestamp;
  311. dst->coded_picture_number = src->coded_picture_number;
  312. dst->display_picture_number = src->display_picture_number;
  313. dst->flags = src->flags;
  314. dst->decode_error_flags = src->decode_error_flags;
  315. dst->color_primaries = src->color_primaries;
  316. dst->color_trc = src->color_trc;
  317. dst->colorspace = src->colorspace;
  318. dst->color_range = src->color_range;
  319. dst->chroma_location = src->chroma_location;
  320. av_dict_copy(&dst->metadata, src->metadata, 0);
  321. #if FF_API_ERROR_FRAME
  322. FF_DISABLE_DEPRECATION_WARNINGS
  323. memcpy(dst->error, src->error, sizeof(dst->error));
  324. FF_ENABLE_DEPRECATION_WARNINGS
  325. #endif
  326. for (i = 0; i < src->nb_side_data; i++) {
  327. const AVFrameSideData *sd_src = src->side_data[i];
  328. AVFrameSideData *sd_dst;
  329. if ( sd_src->type == AV_FRAME_DATA_PANSCAN
  330. && (src->width != dst->width || src->height != dst->height))
  331. continue;
  332. if (force_copy) {
  333. sd_dst = av_frame_new_side_data(dst, sd_src->type,
  334. sd_src->size);
  335. if (!sd_dst) {
  336. wipe_side_data(dst);
  337. return AVERROR(ENOMEM);
  338. }
  339. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  340. } else {
  341. AVBufferRef *ref = av_buffer_ref(sd_src->buf);
  342. sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
  343. if (!sd_dst) {
  344. av_buffer_unref(&ref);
  345. wipe_side_data(dst);
  346. return AVERROR(ENOMEM);
  347. }
  348. }
  349. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  350. }
  351. #if FF_API_FRAME_QP
  352. FF_DISABLE_DEPRECATION_WARNINGS
  353. dst->qscale_table = NULL;
  354. dst->qstride = 0;
  355. dst->qscale_type = 0;
  356. av_buffer_unref(&dst->qp_table_buf);
  357. if (src->qp_table_buf) {
  358. dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
  359. if (dst->qp_table_buf) {
  360. dst->qscale_table = dst->qp_table_buf->data;
  361. dst->qstride = src->qstride;
  362. dst->qscale_type = src->qscale_type;
  363. }
  364. }
  365. FF_ENABLE_DEPRECATION_WARNINGS
  366. #endif
  367. av_buffer_unref(&dst->opaque_ref);
  368. av_buffer_unref(&dst->private_ref);
  369. if (src->opaque_ref) {
  370. dst->opaque_ref = av_buffer_ref(src->opaque_ref);
  371. if (!dst->opaque_ref)
  372. return AVERROR(ENOMEM);
  373. }
  374. if (src->private_ref) {
  375. dst->private_ref = av_buffer_ref(src->private_ref);
  376. if (!dst->private_ref)
  377. return AVERROR(ENOMEM);
  378. }
  379. return 0;
  380. }
  381. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  382. {
  383. int i, ret = 0;
  384. av_assert1(dst->width == 0 && dst->height == 0);
  385. av_assert1(dst->channels == 0);
  386. dst->format = src->format;
  387. dst->width = src->width;
  388. dst->height = src->height;
  389. dst->channels = src->channels;
  390. dst->channel_layout = src->channel_layout;
  391. dst->nb_samples = src->nb_samples;
  392. ret = frame_copy_props(dst, src, 0);
  393. if (ret < 0)
  394. return ret;
  395. /* duplicate the frame data if it's not refcounted */
  396. if (!src->buf[0]) {
  397. ret = av_frame_get_buffer(dst, 32);
  398. if (ret < 0)
  399. return ret;
  400. ret = av_frame_copy(dst, src);
  401. if (ret < 0)
  402. av_frame_unref(dst);
  403. return ret;
  404. }
  405. /* ref the buffers */
  406. for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  407. if (!src->buf[i])
  408. continue;
  409. dst->buf[i] = av_buffer_ref(src->buf[i]);
  410. if (!dst->buf[i]) {
  411. ret = AVERROR(ENOMEM);
  412. goto fail;
  413. }
  414. }
  415. if (src->extended_buf) {
  416. dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
  417. src->nb_extended_buf);
  418. if (!dst->extended_buf) {
  419. ret = AVERROR(ENOMEM);
  420. goto fail;
  421. }
  422. dst->nb_extended_buf = src->nb_extended_buf;
  423. for (i = 0; i < src->nb_extended_buf; i++) {
  424. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  425. if (!dst->extended_buf[i]) {
  426. ret = AVERROR(ENOMEM);
  427. goto fail;
  428. }
  429. }
  430. }
  431. if (src->hw_frames_ctx) {
  432. dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  433. if (!dst->hw_frames_ctx) {
  434. ret = AVERROR(ENOMEM);
  435. goto fail;
  436. }
  437. }
  438. /* duplicate extended data */
  439. if (src->extended_data != src->data) {
  440. int ch = src->channels;
  441. if (!ch) {
  442. ret = AVERROR(EINVAL);
  443. goto fail;
  444. }
  445. CHECK_CHANNELS_CONSISTENCY(src);
  446. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  447. if (!dst->extended_data) {
  448. ret = AVERROR(ENOMEM);
  449. goto fail;
  450. }
  451. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  452. } else
  453. dst->extended_data = dst->data;
  454. memcpy(dst->data, src->data, sizeof(src->data));
  455. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  456. return 0;
  457. fail:
  458. av_frame_unref(dst);
  459. return ret;
  460. }
  461. AVFrame *av_frame_clone(const AVFrame *src)
  462. {
  463. AVFrame *ret = av_frame_alloc();
  464. if (!ret)
  465. return NULL;
  466. if (av_frame_ref(ret, src) < 0)
  467. av_frame_free(&ret);
  468. return ret;
  469. }
  470. void av_frame_unref(AVFrame *frame)
  471. {
  472. int i;
  473. if (!frame)
  474. return;
  475. wipe_side_data(frame);
  476. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  477. av_buffer_unref(&frame->buf[i]);
  478. for (i = 0; i < frame->nb_extended_buf; i++)
  479. av_buffer_unref(&frame->extended_buf[i]);
  480. av_freep(&frame->extended_buf);
  481. av_dict_free(&frame->metadata);
  482. #if FF_API_FRAME_QP
  483. FF_DISABLE_DEPRECATION_WARNINGS
  484. av_buffer_unref(&frame->qp_table_buf);
  485. FF_ENABLE_DEPRECATION_WARNINGS
  486. #endif
  487. av_buffer_unref(&frame->hw_frames_ctx);
  488. av_buffer_unref(&frame->opaque_ref);
  489. av_buffer_unref(&frame->private_ref);
  490. get_frame_defaults(frame);
  491. }
  492. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  493. {
  494. av_assert1(dst->width == 0 && dst->height == 0);
  495. av_assert1(dst->channels == 0);
  496. *dst = *src;
  497. if (src->extended_data == src->data)
  498. dst->extended_data = dst->data;
  499. memset(src, 0, sizeof(*src));
  500. get_frame_defaults(src);
  501. }
  502. int av_frame_is_writable(AVFrame *frame)
  503. {
  504. int i, ret = 1;
  505. /* assume non-refcounted frames are not writable */
  506. if (!frame->buf[0])
  507. return 0;
  508. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  509. if (frame->buf[i])
  510. ret &= !!av_buffer_is_writable(frame->buf[i]);
  511. for (i = 0; i < frame->nb_extended_buf; i++)
  512. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  513. return ret;
  514. }
  515. int av_frame_make_writable(AVFrame *frame)
  516. {
  517. AVFrame tmp;
  518. int ret;
  519. if (!frame->buf[0])
  520. return AVERROR(EINVAL);
  521. if (av_frame_is_writable(frame))
  522. return 0;
  523. memset(&tmp, 0, sizeof(tmp));
  524. tmp.format = frame->format;
  525. tmp.width = frame->width;
  526. tmp.height = frame->height;
  527. tmp.channels = frame->channels;
  528. tmp.channel_layout = frame->channel_layout;
  529. tmp.nb_samples = frame->nb_samples;
  530. ret = av_frame_get_buffer(&tmp, 32);
  531. if (ret < 0)
  532. return ret;
  533. ret = av_frame_copy(&tmp, frame);
  534. if (ret < 0) {
  535. av_frame_unref(&tmp);
  536. return ret;
  537. }
  538. ret = av_frame_copy_props(&tmp, frame);
  539. if (ret < 0) {
  540. av_frame_unref(&tmp);
  541. return ret;
  542. }
  543. av_frame_unref(frame);
  544. *frame = tmp;
  545. if (tmp.data == tmp.extended_data)
  546. frame->extended_data = frame->data;
  547. return 0;
  548. }
  549. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  550. {
  551. return frame_copy_props(dst, src, 1);
  552. }
  553. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  554. {
  555. uint8_t *data;
  556. int planes, i;
  557. if (frame->nb_samples) {
  558. int channels = frame->channels;
  559. if (!channels)
  560. return NULL;
  561. CHECK_CHANNELS_CONSISTENCY(frame);
  562. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  563. } else
  564. planes = 4;
  565. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  566. return NULL;
  567. data = frame->extended_data[plane];
  568. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  569. AVBufferRef *buf = frame->buf[i];
  570. if (data >= buf->data && data < buf->data + buf->size)
  571. return buf;
  572. }
  573. for (i = 0; i < frame->nb_extended_buf; i++) {
  574. AVBufferRef *buf = frame->extended_buf[i];
  575. if (data >= buf->data && data < buf->data + buf->size)
  576. return buf;
  577. }
  578. return NULL;
  579. }
  580. AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame,
  581. enum AVFrameSideDataType type,
  582. AVBufferRef *buf)
  583. {
  584. AVFrameSideData *ret, **tmp;
  585. if (!buf)
  586. return NULL;
  587. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  588. return NULL;
  589. tmp = av_realloc(frame->side_data,
  590. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  591. if (!tmp)
  592. return NULL;
  593. frame->side_data = tmp;
  594. ret = av_mallocz(sizeof(*ret));
  595. if (!ret)
  596. return NULL;
  597. ret->buf = buf;
  598. ret->data = ret->buf->data;
  599. ret->size = buf->size;
  600. ret->type = type;
  601. frame->side_data[frame->nb_side_data++] = ret;
  602. return ret;
  603. }
  604. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  605. enum AVFrameSideDataType type,
  606. int size)
  607. {
  608. AVFrameSideData *ret;
  609. AVBufferRef *buf = av_buffer_alloc(size);
  610. ret = av_frame_new_side_data_from_buf(frame, type, buf);
  611. if (!ret)
  612. av_buffer_unref(&buf);
  613. return ret;
  614. }
  615. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  616. enum AVFrameSideDataType type)
  617. {
  618. int i;
  619. for (i = 0; i < frame->nb_side_data; i++) {
  620. if (frame->side_data[i]->type == type)
  621. return frame->side_data[i];
  622. }
  623. return NULL;
  624. }
  625. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  626. {
  627. const uint8_t *src_data[4];
  628. int i, planes;
  629. if (dst->width < src->width ||
  630. dst->height < src->height)
  631. return AVERROR(EINVAL);
  632. planes = av_pix_fmt_count_planes(dst->format);
  633. for (i = 0; i < planes; i++)
  634. if (!dst->data[i] || !src->data[i])
  635. return AVERROR(EINVAL);
  636. memcpy(src_data, src->data, sizeof(src_data));
  637. av_image_copy(dst->data, dst->linesize,
  638. src_data, src->linesize,
  639. dst->format, src->width, src->height);
  640. return 0;
  641. }
  642. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  643. {
  644. int planar = av_sample_fmt_is_planar(dst->format);
  645. int channels = dst->channels;
  646. int planes = planar ? channels : 1;
  647. int i;
  648. if (dst->nb_samples != src->nb_samples ||
  649. dst->channels != src->channels ||
  650. dst->channel_layout != src->channel_layout)
  651. return AVERROR(EINVAL);
  652. CHECK_CHANNELS_CONSISTENCY(src);
  653. for (i = 0; i < planes; i++)
  654. if (!dst->extended_data[i] || !src->extended_data[i])
  655. return AVERROR(EINVAL);
  656. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  657. dst->nb_samples, channels, dst->format);
  658. return 0;
  659. }
  660. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  661. {
  662. if (dst->format != src->format || dst->format < 0)
  663. return AVERROR(EINVAL);
  664. if (dst->width > 0 && dst->height > 0)
  665. return frame_copy_video(dst, src);
  666. else if (dst->nb_samples > 0 && dst->channels > 0)
  667. return frame_copy_audio(dst, src);
  668. return AVERROR(EINVAL);
  669. }
  670. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  671. {
  672. int i;
  673. for (i = 0; i < frame->nb_side_data; i++) {
  674. AVFrameSideData *sd = frame->side_data[i];
  675. if (sd->type == type) {
  676. free_side_data(&frame->side_data[i]);
  677. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  678. frame->nb_side_data--;
  679. }
  680. }
  681. }
  682. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  683. {
  684. switch(type) {
  685. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  686. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  687. case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
  688. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  689. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  690. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  691. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  692. case AV_FRAME_DATA_AFD: return "Active format description";
  693. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  694. case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
  695. case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
  696. case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
  697. case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
  698. case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
  699. case AV_FRAME_DATA_ICC_PROFILE: return "ICC profile";
  700. case AV_FRAME_DATA_QP_TABLE_PROPERTIES: return "QP table properties";
  701. case AV_FRAME_DATA_QP_TABLE_DATA: return "QP table data";
  702. }
  703. return NULL;
  704. }
  705. static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
  706. const AVPixFmtDescriptor *desc)
  707. {
  708. int i, j;
  709. for (i = 0; frame->data[i]; i++) {
  710. const AVComponentDescriptor *comp = NULL;
  711. int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
  712. int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  713. if (desc->flags & (AV_PIX_FMT_FLAG_PAL | FF_PSEUDOPAL) && i == 1) {
  714. offsets[i] = 0;
  715. break;
  716. }
  717. /* find any component descriptor for this plane */
  718. for (j = 0; j < desc->nb_components; j++) {
  719. if (desc->comp[j].plane == i) {
  720. comp = &desc->comp[j];
  721. break;
  722. }
  723. }
  724. if (!comp)
  725. return AVERROR_BUG;
  726. offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
  727. (frame->crop_left >> shift_x) * comp->step;
  728. }
  729. return 0;
  730. }
  731. int av_frame_apply_cropping(AVFrame *frame, int flags)
  732. {
  733. const AVPixFmtDescriptor *desc;
  734. size_t offsets[4];
  735. int i;
  736. if (!(frame->width > 0 && frame->height > 0))
  737. return AVERROR(EINVAL);
  738. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  739. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  740. (frame->crop_left + frame->crop_right) >= frame->width ||
  741. (frame->crop_top + frame->crop_bottom) >= frame->height)
  742. return AVERROR(ERANGE);
  743. desc = av_pix_fmt_desc_get(frame->format);
  744. if (!desc)
  745. return AVERROR_BUG;
  746. /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
  747. * formats cannot be easily handled here either (and corresponding decoders
  748. * should not export any cropping anyway), so do the same for those as well.
  749. * */
  750. if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
  751. frame->width -= frame->crop_right;
  752. frame->height -= frame->crop_bottom;
  753. frame->crop_right = 0;
  754. frame->crop_bottom = 0;
  755. return 0;
  756. }
  757. /* calculate the offsets for each plane */
  758. calc_cropping_offsets(offsets, frame, desc);
  759. /* adjust the offsets to avoid breaking alignment */
  760. if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
  761. int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
  762. int min_log2_align = INT_MAX;
  763. for (i = 0; frame->data[i]; i++) {
  764. int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
  765. min_log2_align = FFMIN(log2_align, min_log2_align);
  766. }
  767. /* we assume, and it should always be true, that the data alignment is
  768. * related to the cropping alignment by a constant power-of-2 factor */
  769. if (log2_crop_align < min_log2_align)
  770. return AVERROR_BUG;
  771. if (min_log2_align < 5) {
  772. frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
  773. calc_cropping_offsets(offsets, frame, desc);
  774. }
  775. }
  776. for (i = 0; frame->data[i]; i++)
  777. frame->data[i] += offsets[i];
  778. frame->width -= (frame->crop_left + frame->crop_right);
  779. frame->height -= (frame->crop_top + frame->crop_bottom);
  780. frame->crop_left = 0;
  781. frame->crop_right = 0;
  782. frame->crop_top = 0;
  783. frame->crop_bottom = 0;
  784. return 0;
  785. }