2
0

frame.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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, padded_height;
  183. int plane_padding = FFMAX(16 + 16/*STRIDE_ALIGN*/, align);
  184. if (!desc)
  185. return AVERROR(EINVAL);
  186. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  187. return ret;
  188. if (!frame->linesize[0]) {
  189. if (align <= 0)
  190. align = 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */
  191. for(i=1; i<=align; i+=i) {
  192. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  193. FFALIGN(frame->width, i));
  194. if (ret < 0)
  195. return ret;
  196. if (!(frame->linesize[0] & (align-1)))
  197. break;
  198. }
  199. for (i = 0; i < 4 && frame->linesize[i]; i++)
  200. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  201. }
  202. padded_height = FFALIGN(frame->height, 32);
  203. if ((ret = av_image_fill_pointers(frame->data, frame->format, padded_height,
  204. NULL, frame->linesize)) < 0)
  205. return ret;
  206. frame->buf[0] = av_buffer_alloc(ret + 4*plane_padding);
  207. if (!frame->buf[0]) {
  208. ret = AVERROR(ENOMEM);
  209. goto fail;
  210. }
  211. if ((ret = av_image_fill_pointers(frame->data, frame->format, padded_height,
  212. frame->buf[0]->data, frame->linesize)) < 0)
  213. goto fail;
  214. for (i = 1; i < 4; i++) {
  215. if (frame->data[i])
  216. frame->data[i] += i * plane_padding;
  217. }
  218. frame->extended_data = frame->data;
  219. return 0;
  220. fail:
  221. av_frame_unref(frame);
  222. return ret;
  223. }
  224. static int get_audio_buffer(AVFrame *frame, int align)
  225. {
  226. int channels;
  227. int planar = av_sample_fmt_is_planar(frame->format);
  228. int planes;
  229. int ret, i;
  230. if (!frame->channels)
  231. frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  232. channels = frame->channels;
  233. planes = planar ? channels : 1;
  234. CHECK_CHANNELS_CONSISTENCY(frame);
  235. if (!frame->linesize[0]) {
  236. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  237. frame->nb_samples, frame->format,
  238. align);
  239. if (ret < 0)
  240. return ret;
  241. }
  242. if (planes > AV_NUM_DATA_POINTERS) {
  243. frame->extended_data = av_mallocz_array(planes,
  244. sizeof(*frame->extended_data));
  245. frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
  246. sizeof(*frame->extended_buf));
  247. if (!frame->extended_data || !frame->extended_buf) {
  248. av_freep(&frame->extended_data);
  249. av_freep(&frame->extended_buf);
  250. return AVERROR(ENOMEM);
  251. }
  252. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  253. } else
  254. frame->extended_data = frame->data;
  255. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  256. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  257. if (!frame->buf[i]) {
  258. av_frame_unref(frame);
  259. return AVERROR(ENOMEM);
  260. }
  261. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  262. }
  263. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  264. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  265. if (!frame->extended_buf[i]) {
  266. av_frame_unref(frame);
  267. return AVERROR(ENOMEM);
  268. }
  269. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  270. }
  271. return 0;
  272. }
  273. int av_frame_get_buffer(AVFrame *frame, int align)
  274. {
  275. if (frame->format < 0)
  276. return AVERROR(EINVAL);
  277. if (frame->width > 0 && frame->height > 0)
  278. return get_video_buffer(frame, align);
  279. else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
  280. return get_audio_buffer(frame, align);
  281. return AVERROR(EINVAL);
  282. }
  283. static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
  284. {
  285. int i;
  286. dst->key_frame = src->key_frame;
  287. dst->pict_type = src->pict_type;
  288. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  289. dst->crop_top = src->crop_top;
  290. dst->crop_bottom = src->crop_bottom;
  291. dst->crop_left = src->crop_left;
  292. dst->crop_right = src->crop_right;
  293. dst->pts = src->pts;
  294. dst->repeat_pict = src->repeat_pict;
  295. dst->interlaced_frame = src->interlaced_frame;
  296. dst->top_field_first = src->top_field_first;
  297. dst->palette_has_changed = src->palette_has_changed;
  298. dst->sample_rate = src->sample_rate;
  299. dst->opaque = src->opaque;
  300. #if FF_API_PKT_PTS
  301. FF_DISABLE_DEPRECATION_WARNINGS
  302. dst->pkt_pts = src->pkt_pts;
  303. FF_ENABLE_DEPRECATION_WARNINGS
  304. #endif
  305. dst->pkt_dts = src->pkt_dts;
  306. dst->pkt_pos = src->pkt_pos;
  307. dst->pkt_size = src->pkt_size;
  308. dst->pkt_duration = src->pkt_duration;
  309. dst->reordered_opaque = src->reordered_opaque;
  310. dst->quality = src->quality;
  311. dst->best_effort_timestamp = src->best_effort_timestamp;
  312. dst->coded_picture_number = src->coded_picture_number;
  313. dst->display_picture_number = src->display_picture_number;
  314. dst->flags = src->flags;
  315. dst->decode_error_flags = src->decode_error_flags;
  316. dst->color_primaries = src->color_primaries;
  317. dst->color_trc = src->color_trc;
  318. dst->colorspace = src->colorspace;
  319. dst->color_range = src->color_range;
  320. dst->chroma_location = src->chroma_location;
  321. av_dict_copy(&dst->metadata, src->metadata, 0);
  322. #if FF_API_ERROR_FRAME
  323. FF_DISABLE_DEPRECATION_WARNINGS
  324. memcpy(dst->error, src->error, sizeof(dst->error));
  325. FF_ENABLE_DEPRECATION_WARNINGS
  326. #endif
  327. for (i = 0; i < src->nb_side_data; i++) {
  328. const AVFrameSideData *sd_src = src->side_data[i];
  329. AVFrameSideData *sd_dst;
  330. if ( sd_src->type == AV_FRAME_DATA_PANSCAN
  331. && (src->width != dst->width || src->height != dst->height))
  332. continue;
  333. if (force_copy) {
  334. sd_dst = av_frame_new_side_data(dst, sd_src->type,
  335. sd_src->size);
  336. if (!sd_dst) {
  337. wipe_side_data(dst);
  338. return AVERROR(ENOMEM);
  339. }
  340. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  341. } else {
  342. AVBufferRef *ref = av_buffer_ref(sd_src->buf);
  343. sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
  344. if (!sd_dst) {
  345. av_buffer_unref(&ref);
  346. wipe_side_data(dst);
  347. return AVERROR(ENOMEM);
  348. }
  349. }
  350. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  351. }
  352. #if FF_API_FRAME_QP
  353. FF_DISABLE_DEPRECATION_WARNINGS
  354. dst->qscale_table = NULL;
  355. dst->qstride = 0;
  356. dst->qscale_type = 0;
  357. av_buffer_unref(&dst->qp_table_buf);
  358. if (src->qp_table_buf) {
  359. dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
  360. if (dst->qp_table_buf) {
  361. dst->qscale_table = dst->qp_table_buf->data;
  362. dst->qstride = src->qstride;
  363. dst->qscale_type = src->qscale_type;
  364. }
  365. }
  366. FF_ENABLE_DEPRECATION_WARNINGS
  367. #endif
  368. av_buffer_unref(&dst->opaque_ref);
  369. av_buffer_unref(&dst->private_ref);
  370. if (src->opaque_ref) {
  371. dst->opaque_ref = av_buffer_ref(src->opaque_ref);
  372. if (!dst->opaque_ref)
  373. return AVERROR(ENOMEM);
  374. }
  375. if (src->private_ref) {
  376. dst->private_ref = av_buffer_ref(src->private_ref);
  377. if (!dst->private_ref)
  378. return AVERROR(ENOMEM);
  379. }
  380. return 0;
  381. }
  382. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  383. {
  384. int i, ret = 0;
  385. av_assert1(dst->width == 0 && dst->height == 0);
  386. av_assert1(dst->channels == 0);
  387. dst->format = src->format;
  388. dst->width = src->width;
  389. dst->height = src->height;
  390. dst->channels = src->channels;
  391. dst->channel_layout = src->channel_layout;
  392. dst->nb_samples = src->nb_samples;
  393. ret = frame_copy_props(dst, src, 0);
  394. if (ret < 0)
  395. return ret;
  396. /* duplicate the frame data if it's not refcounted */
  397. if (!src->buf[0]) {
  398. ret = av_frame_get_buffer(dst, 32);
  399. if (ret < 0)
  400. return ret;
  401. ret = av_frame_copy(dst, src);
  402. if (ret < 0)
  403. av_frame_unref(dst);
  404. return ret;
  405. }
  406. /* ref the buffers */
  407. for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  408. if (!src->buf[i])
  409. continue;
  410. dst->buf[i] = av_buffer_ref(src->buf[i]);
  411. if (!dst->buf[i]) {
  412. ret = AVERROR(ENOMEM);
  413. goto fail;
  414. }
  415. }
  416. if (src->extended_buf) {
  417. dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
  418. src->nb_extended_buf);
  419. if (!dst->extended_buf) {
  420. ret = AVERROR(ENOMEM);
  421. goto fail;
  422. }
  423. dst->nb_extended_buf = src->nb_extended_buf;
  424. for (i = 0; i < src->nb_extended_buf; i++) {
  425. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  426. if (!dst->extended_buf[i]) {
  427. ret = AVERROR(ENOMEM);
  428. goto fail;
  429. }
  430. }
  431. }
  432. if (src->hw_frames_ctx) {
  433. dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  434. if (!dst->hw_frames_ctx) {
  435. ret = AVERROR(ENOMEM);
  436. goto fail;
  437. }
  438. }
  439. /* duplicate extended data */
  440. if (src->extended_data != src->data) {
  441. int ch = src->channels;
  442. if (!ch) {
  443. ret = AVERROR(EINVAL);
  444. goto fail;
  445. }
  446. CHECK_CHANNELS_CONSISTENCY(src);
  447. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  448. if (!dst->extended_data) {
  449. ret = AVERROR(ENOMEM);
  450. goto fail;
  451. }
  452. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  453. } else
  454. dst->extended_data = dst->data;
  455. memcpy(dst->data, src->data, sizeof(src->data));
  456. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  457. return 0;
  458. fail:
  459. av_frame_unref(dst);
  460. return ret;
  461. }
  462. AVFrame *av_frame_clone(const AVFrame *src)
  463. {
  464. AVFrame *ret = av_frame_alloc();
  465. if (!ret)
  466. return NULL;
  467. if (av_frame_ref(ret, src) < 0)
  468. av_frame_free(&ret);
  469. return ret;
  470. }
  471. void av_frame_unref(AVFrame *frame)
  472. {
  473. int i;
  474. if (!frame)
  475. return;
  476. wipe_side_data(frame);
  477. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  478. av_buffer_unref(&frame->buf[i]);
  479. for (i = 0; i < frame->nb_extended_buf; i++)
  480. av_buffer_unref(&frame->extended_buf[i]);
  481. av_freep(&frame->extended_buf);
  482. av_dict_free(&frame->metadata);
  483. #if FF_API_FRAME_QP
  484. FF_DISABLE_DEPRECATION_WARNINGS
  485. av_buffer_unref(&frame->qp_table_buf);
  486. FF_ENABLE_DEPRECATION_WARNINGS
  487. #endif
  488. av_buffer_unref(&frame->hw_frames_ctx);
  489. av_buffer_unref(&frame->opaque_ref);
  490. av_buffer_unref(&frame->private_ref);
  491. get_frame_defaults(frame);
  492. }
  493. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  494. {
  495. av_assert1(dst->width == 0 && dst->height == 0);
  496. av_assert1(dst->channels == 0);
  497. *dst = *src;
  498. if (src->extended_data == src->data)
  499. dst->extended_data = dst->data;
  500. memset(src, 0, sizeof(*src));
  501. get_frame_defaults(src);
  502. }
  503. int av_frame_is_writable(AVFrame *frame)
  504. {
  505. int i, ret = 1;
  506. /* assume non-refcounted frames are not writable */
  507. if (!frame->buf[0])
  508. return 0;
  509. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  510. if (frame->buf[i])
  511. ret &= !!av_buffer_is_writable(frame->buf[i]);
  512. for (i = 0; i < frame->nb_extended_buf; i++)
  513. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  514. return ret;
  515. }
  516. int av_frame_make_writable(AVFrame *frame)
  517. {
  518. AVFrame tmp;
  519. int ret;
  520. if (!frame->buf[0])
  521. return AVERROR(EINVAL);
  522. if (av_frame_is_writable(frame))
  523. return 0;
  524. memset(&tmp, 0, sizeof(tmp));
  525. tmp.format = frame->format;
  526. tmp.width = frame->width;
  527. tmp.height = frame->height;
  528. tmp.channels = frame->channels;
  529. tmp.channel_layout = frame->channel_layout;
  530. tmp.nb_samples = frame->nb_samples;
  531. ret = av_frame_get_buffer(&tmp, 32);
  532. if (ret < 0)
  533. return ret;
  534. ret = av_frame_copy(&tmp, frame);
  535. if (ret < 0) {
  536. av_frame_unref(&tmp);
  537. return ret;
  538. }
  539. ret = av_frame_copy_props(&tmp, frame);
  540. if (ret < 0) {
  541. av_frame_unref(&tmp);
  542. return ret;
  543. }
  544. av_frame_unref(frame);
  545. *frame = tmp;
  546. if (tmp.data == tmp.extended_data)
  547. frame->extended_data = frame->data;
  548. return 0;
  549. }
  550. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  551. {
  552. return frame_copy_props(dst, src, 1);
  553. }
  554. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  555. {
  556. uint8_t *data;
  557. int planes, i;
  558. if (frame->nb_samples) {
  559. int channels = frame->channels;
  560. if (!channels)
  561. return NULL;
  562. CHECK_CHANNELS_CONSISTENCY(frame);
  563. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  564. } else
  565. planes = 4;
  566. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  567. return NULL;
  568. data = frame->extended_data[plane];
  569. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  570. AVBufferRef *buf = frame->buf[i];
  571. if (data >= buf->data && data < buf->data + buf->size)
  572. return buf;
  573. }
  574. for (i = 0; i < frame->nb_extended_buf; i++) {
  575. AVBufferRef *buf = frame->extended_buf[i];
  576. if (data >= buf->data && data < buf->data + buf->size)
  577. return buf;
  578. }
  579. return NULL;
  580. }
  581. AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame,
  582. enum AVFrameSideDataType type,
  583. AVBufferRef *buf)
  584. {
  585. AVFrameSideData *ret, **tmp;
  586. if (!buf)
  587. return NULL;
  588. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  589. return NULL;
  590. tmp = av_realloc(frame->side_data,
  591. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  592. if (!tmp)
  593. return NULL;
  594. frame->side_data = tmp;
  595. ret = av_mallocz(sizeof(*ret));
  596. if (!ret)
  597. return NULL;
  598. ret->buf = buf;
  599. ret->data = ret->buf->data;
  600. ret->size = buf->size;
  601. ret->type = type;
  602. frame->side_data[frame->nb_side_data++] = ret;
  603. return ret;
  604. }
  605. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  606. enum AVFrameSideDataType type,
  607. int size)
  608. {
  609. AVFrameSideData *ret;
  610. AVBufferRef *buf = av_buffer_alloc(size);
  611. ret = av_frame_new_side_data_from_buf(frame, type, buf);
  612. if (!ret)
  613. av_buffer_unref(&buf);
  614. return ret;
  615. }
  616. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  617. enum AVFrameSideDataType type)
  618. {
  619. int i;
  620. for (i = 0; i < frame->nb_side_data; i++) {
  621. if (frame->side_data[i]->type == type)
  622. return frame->side_data[i];
  623. }
  624. return NULL;
  625. }
  626. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  627. {
  628. const uint8_t *src_data[4];
  629. int i, planes;
  630. if (dst->width < src->width ||
  631. dst->height < src->height)
  632. return AVERROR(EINVAL);
  633. planes = av_pix_fmt_count_planes(dst->format);
  634. for (i = 0; i < planes; i++)
  635. if (!dst->data[i] || !src->data[i])
  636. return AVERROR(EINVAL);
  637. memcpy(src_data, src->data, sizeof(src_data));
  638. av_image_copy(dst->data, dst->linesize,
  639. src_data, src->linesize,
  640. dst->format, src->width, src->height);
  641. return 0;
  642. }
  643. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  644. {
  645. int planar = av_sample_fmt_is_planar(dst->format);
  646. int channels = dst->channels;
  647. int planes = planar ? channels : 1;
  648. int i;
  649. if (dst->nb_samples != src->nb_samples ||
  650. dst->channels != src->channels ||
  651. dst->channel_layout != src->channel_layout)
  652. return AVERROR(EINVAL);
  653. CHECK_CHANNELS_CONSISTENCY(src);
  654. for (i = 0; i < planes; i++)
  655. if (!dst->extended_data[i] || !src->extended_data[i])
  656. return AVERROR(EINVAL);
  657. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  658. dst->nb_samples, channels, dst->format);
  659. return 0;
  660. }
  661. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  662. {
  663. if (dst->format != src->format || dst->format < 0)
  664. return AVERROR(EINVAL);
  665. if (dst->width > 0 && dst->height > 0)
  666. return frame_copy_video(dst, src);
  667. else if (dst->nb_samples > 0 && dst->channels > 0)
  668. return frame_copy_audio(dst, src);
  669. return AVERROR(EINVAL);
  670. }
  671. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  672. {
  673. int i;
  674. for (i = 0; i < frame->nb_side_data; i++) {
  675. AVFrameSideData *sd = frame->side_data[i];
  676. if (sd->type == type) {
  677. free_side_data(&frame->side_data[i]);
  678. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  679. frame->nb_side_data--;
  680. }
  681. }
  682. }
  683. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  684. {
  685. switch(type) {
  686. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  687. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  688. case AV_FRAME_DATA_STEREO3D: return "Stereo 3D";
  689. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  690. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  691. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  692. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  693. case AV_FRAME_DATA_AFD: return "Active format description";
  694. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  695. case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
  696. case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
  697. case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
  698. case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
  699. case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
  700. case AV_FRAME_DATA_S12M_TIMECODE: return "SMPTE 12-1 timecode";
  701. case AV_FRAME_DATA_SPHERICAL: return "Spherical Mapping";
  702. case AV_FRAME_DATA_ICC_PROFILE: return "ICC profile";
  703. #if FF_API_FRAME_QP
  704. case AV_FRAME_DATA_QP_TABLE_PROPERTIES: return "QP table properties";
  705. case AV_FRAME_DATA_QP_TABLE_DATA: return "QP table data";
  706. #endif
  707. }
  708. return NULL;
  709. }
  710. static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
  711. const AVPixFmtDescriptor *desc)
  712. {
  713. int i, j;
  714. for (i = 0; frame->data[i]; i++) {
  715. const AVComponentDescriptor *comp = NULL;
  716. int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
  717. int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  718. if (desc->flags & (AV_PIX_FMT_FLAG_PAL | FF_PSEUDOPAL) && i == 1) {
  719. offsets[i] = 0;
  720. break;
  721. }
  722. /* find any component descriptor for this plane */
  723. for (j = 0; j < desc->nb_components; j++) {
  724. if (desc->comp[j].plane == i) {
  725. comp = &desc->comp[j];
  726. break;
  727. }
  728. }
  729. if (!comp)
  730. return AVERROR_BUG;
  731. offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
  732. (frame->crop_left >> shift_x) * comp->step;
  733. }
  734. return 0;
  735. }
  736. int av_frame_apply_cropping(AVFrame *frame, int flags)
  737. {
  738. const AVPixFmtDescriptor *desc;
  739. size_t offsets[4];
  740. int i;
  741. if (!(frame->width > 0 && frame->height > 0))
  742. return AVERROR(EINVAL);
  743. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  744. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  745. (frame->crop_left + frame->crop_right) >= frame->width ||
  746. (frame->crop_top + frame->crop_bottom) >= frame->height)
  747. return AVERROR(ERANGE);
  748. desc = av_pix_fmt_desc_get(frame->format);
  749. if (!desc)
  750. return AVERROR_BUG;
  751. /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
  752. * formats cannot be easily handled here either (and corresponding decoders
  753. * should not export any cropping anyway), so do the same for those as well.
  754. * */
  755. if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
  756. frame->width -= frame->crop_right;
  757. frame->height -= frame->crop_bottom;
  758. frame->crop_right = 0;
  759. frame->crop_bottom = 0;
  760. return 0;
  761. }
  762. /* calculate the offsets for each plane */
  763. calc_cropping_offsets(offsets, frame, desc);
  764. /* adjust the offsets to avoid breaking alignment */
  765. if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
  766. int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
  767. int min_log2_align = INT_MAX;
  768. for (i = 0; frame->data[i]; i++) {
  769. int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
  770. min_log2_align = FFMIN(log2_align, min_log2_align);
  771. }
  772. /* we assume, and it should always be true, that the data alignment is
  773. * related to the cropping alignment by a constant power-of-2 factor */
  774. if (log2_crop_align < min_log2_align)
  775. return AVERROR_BUG;
  776. if (min_log2_align < 5) {
  777. frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
  778. calc_cropping_offsets(offsets, frame, desc);
  779. }
  780. }
  781. for (i = 0; frame->data[i]; i++)
  782. frame->data[i] += offsets[i];
  783. frame->width -= (frame->crop_left + frame->crop_right);
  784. frame->height -= (frame->crop_top + frame->crop_bottom);
  785. frame->crop_left = 0;
  786. frame->crop_right = 0;
  787. frame->crop_top = 0;
  788. frame->crop_bottom = 0;
  789. return 0;
  790. }