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.

1352 lines
39KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "libavutil/opt.h"
  36. #include "imgconvert.h"
  37. #include "thread.h"
  38. #include "audioconvert.h"
  39. #include "internal.h"
  40. #include <stdlib.h>
  41. #include <stdarg.h>
  42. #include <limits.h>
  43. #include <float.h>
  44. static int volatile entangled_thread_counter=0;
  45. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  46. static void *codec_mutex;
  47. void *av_fast_realloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
  48. {
  49. if(min_size < *size)
  50. return ptr;
  51. min_size= FFMAX(17*min_size/16 + 32, min_size);
  52. ptr= av_realloc(ptr, min_size);
  53. if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
  54. min_size= 0;
  55. *size= min_size;
  56. return ptr;
  57. }
  58. void av_fast_malloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
  59. {
  60. void **p = ptr;
  61. if (min_size < *size)
  62. return;
  63. min_size= FFMAX(17*min_size/16 + 32, min_size);
  64. av_free(*p);
  65. *p = av_malloc(min_size);
  66. if (!*p) min_size = 0;
  67. *size= min_size;
  68. }
  69. /* encoder management */
  70. static AVCodec *first_avcodec = NULL;
  71. AVCodec *av_codec_next(AVCodec *c){
  72. if(c) return c->next;
  73. else return first_avcodec;
  74. }
  75. void avcodec_register(AVCodec *codec)
  76. {
  77. AVCodec **p;
  78. avcodec_init();
  79. p = &first_avcodec;
  80. while (*p != NULL) p = &(*p)->next;
  81. *p = codec;
  82. codec->next = NULL;
  83. }
  84. #if LIBAVCODEC_VERSION_MAJOR < 53
  85. void register_avcodec(AVCodec *codec)
  86. {
  87. avcodec_register(codec);
  88. }
  89. #endif
  90. unsigned avcodec_get_edge_width(void)
  91. {
  92. return EDGE_WIDTH;
  93. }
  94. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  95. s->coded_width = width;
  96. s->coded_height= height;
  97. s->width = -((-width )>>s->lowres);
  98. s->height= -((-height)>>s->lowres);
  99. }
  100. typedef struct InternalBuffer{
  101. int last_pic_num;
  102. uint8_t *base[4];
  103. uint8_t *data[4];
  104. int linesize[4];
  105. int width, height;
  106. enum PixelFormat pix_fmt;
  107. }InternalBuffer;
  108. #define INTERNAL_BUFFER_SIZE 32
  109. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  110. int w_align= 1;
  111. int h_align= 1;
  112. switch(s->pix_fmt){
  113. case PIX_FMT_YUV420P:
  114. case PIX_FMT_YUYV422:
  115. case PIX_FMT_UYVY422:
  116. case PIX_FMT_YUV422P:
  117. case PIX_FMT_YUV440P:
  118. case PIX_FMT_YUV444P:
  119. case PIX_FMT_GRAY8:
  120. case PIX_FMT_GRAY16BE:
  121. case PIX_FMT_GRAY16LE:
  122. case PIX_FMT_YUVJ420P:
  123. case PIX_FMT_YUVJ422P:
  124. case PIX_FMT_YUVJ440P:
  125. case PIX_FMT_YUVJ444P:
  126. case PIX_FMT_YUVA420P:
  127. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  128. h_align= 16;
  129. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
  130. h_align= 32; // interlaced is rounded up to 2 MBs
  131. break;
  132. case PIX_FMT_YUV411P:
  133. case PIX_FMT_UYYVYY411:
  134. w_align=32;
  135. h_align=8;
  136. break;
  137. case PIX_FMT_YUV410P:
  138. if(s->codec_id == CODEC_ID_SVQ1){
  139. w_align=64;
  140. h_align=64;
  141. }
  142. case PIX_FMT_RGB555:
  143. if(s->codec_id == CODEC_ID_RPZA){
  144. w_align=4;
  145. h_align=4;
  146. }
  147. case PIX_FMT_PAL8:
  148. case PIX_FMT_BGR8:
  149. case PIX_FMT_RGB8:
  150. if(s->codec_id == CODEC_ID_SMC){
  151. w_align=4;
  152. h_align=4;
  153. }
  154. break;
  155. case PIX_FMT_BGR24:
  156. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  157. w_align=4;
  158. h_align=4;
  159. }
  160. break;
  161. default:
  162. w_align= 1;
  163. h_align= 1;
  164. break;
  165. }
  166. *width = FFALIGN(*width , w_align);
  167. *height= FFALIGN(*height, h_align);
  168. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  169. *height+=2; // some of the optimized chroma MC reads one line too much
  170. // which is also done in mpeg decoders with lowres > 0
  171. linesize_align[0] =
  172. linesize_align[1] =
  173. linesize_align[2] =
  174. linesize_align[3] = STRIDE_ALIGN;
  175. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  176. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  177. //picture size unneccessarily in some cases. The solution here is not
  178. //pretty and better ideas are welcome!
  179. #if HAVE_MMX
  180. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  181. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  182. s->codec_id == CODEC_ID_VP6A) {
  183. linesize_align[0] =
  184. linesize_align[1] =
  185. linesize_align[2] = 16;
  186. }
  187. #endif
  188. }
  189. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  190. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  191. int linesize_align[4];
  192. int align;
  193. avcodec_align_dimensions2(s, width, height, linesize_align);
  194. align = FFMAX(linesize_align[0], linesize_align[3]);
  195. linesize_align[1] <<= chroma_shift;
  196. linesize_align[2] <<= chroma_shift;
  197. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  198. *width=FFALIGN(*width, align);
  199. }
  200. #if LIBAVCODEC_VERSION_MAJOR < 53
  201. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  202. return av_image_check_size(w, h, 0, av_log_ctx);
  203. }
  204. #endif
  205. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  206. int i;
  207. int w= s->width;
  208. int h= s->height;
  209. InternalBuffer *buf;
  210. int *picture_number;
  211. if(pic->data[0]!=NULL) {
  212. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  213. return -1;
  214. }
  215. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  216. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  217. return -1;
  218. }
  219. if(av_image_check_size(w, h, 0, s))
  220. return -1;
  221. if(s->internal_buffer==NULL){
  222. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  223. }
  224. #if 0
  225. s->internal_buffer= av_fast_realloc(
  226. s->internal_buffer,
  227. &s->internal_buffer_size,
  228. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  229. );
  230. #endif
  231. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  232. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  233. (*picture_number)++;
  234. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  235. if(s->active_thread_type&FF_THREAD_FRAME) {
  236. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  237. return -1;
  238. }
  239. for(i=0; i<4; i++){
  240. av_freep(&buf->base[i]);
  241. buf->data[i]= NULL;
  242. }
  243. }
  244. if(buf->base[0]){
  245. pic->age= *picture_number - buf->last_pic_num;
  246. buf->last_pic_num= *picture_number;
  247. }else{
  248. int h_chroma_shift, v_chroma_shift;
  249. int size[4] = {0};
  250. int tmpsize;
  251. int unaligned;
  252. AVPicture picture;
  253. int stride_align[4];
  254. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  255. avcodec_align_dimensions2(s, &w, &h, stride_align);
  256. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  257. w+= EDGE_WIDTH*2;
  258. h+= EDGE_WIDTH*2;
  259. }
  260. do {
  261. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  262. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  263. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  264. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  265. w += w & ~(w-1);
  266. unaligned = 0;
  267. for (i=0; i<4; i++){
  268. unaligned |= picture.linesize[i] % stride_align[i];
  269. }
  270. } while (unaligned);
  271. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  272. if (tmpsize < 0)
  273. return -1;
  274. for (i=0; i<3 && picture.data[i+1]; i++)
  275. size[i] = picture.data[i+1] - picture.data[i];
  276. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  277. buf->last_pic_num= -256*256*256*64;
  278. memset(buf->base, 0, sizeof(buf->base));
  279. memset(buf->data, 0, sizeof(buf->data));
  280. for(i=0; i<4 && size[i]; i++){
  281. const int h_shift= i==0 ? 0 : h_chroma_shift;
  282. const int v_shift= i==0 ? 0 : v_chroma_shift;
  283. buf->linesize[i]= picture.linesize[i];
  284. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  285. if(buf->base[i]==NULL) return -1;
  286. memset(buf->base[i], 128, size[i]);
  287. // no edge if EDGE EMU or not planar YUV
  288. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  289. buf->data[i] = buf->base[i];
  290. else
  291. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  292. }
  293. if(size[1] && !size[2])
  294. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  295. buf->width = s->width;
  296. buf->height = s->height;
  297. buf->pix_fmt= s->pix_fmt;
  298. pic->age= 256*256*256*64;
  299. }
  300. pic->type= FF_BUFFER_TYPE_INTERNAL;
  301. for(i=0; i<4; i++){
  302. pic->base[i]= buf->base[i];
  303. pic->data[i]= buf->data[i];
  304. pic->linesize[i]= buf->linesize[i];
  305. }
  306. s->internal_buffer_count++;
  307. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  308. else pic->pkt_pts= AV_NOPTS_VALUE;
  309. pic->reordered_opaque= s->reordered_opaque;
  310. if(s->debug&FF_DEBUG_BUFFERS)
  311. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  312. return 0;
  313. }
  314. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  315. int i;
  316. InternalBuffer *buf, *last;
  317. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  318. assert(s->internal_buffer_count);
  319. if(s->internal_buffer){
  320. buf = NULL; /* avoids warning */
  321. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  322. buf= &((InternalBuffer*)s->internal_buffer)[i];
  323. if(buf->data[0] == pic->data[0])
  324. break;
  325. }
  326. assert(i < s->internal_buffer_count);
  327. s->internal_buffer_count--;
  328. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  329. FFSWAP(InternalBuffer, *buf, *last);
  330. }
  331. for(i=0; i<4; i++){
  332. pic->data[i]=NULL;
  333. // pic->base[i]=NULL;
  334. }
  335. //printf("R%X\n", pic->opaque);
  336. if(s->debug&FF_DEBUG_BUFFERS)
  337. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  338. }
  339. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  340. AVFrame temp_pic;
  341. int i;
  342. /* If no picture return a new buffer */
  343. if(pic->data[0] == NULL) {
  344. /* We will copy from buffer, so must be readable */
  345. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  346. return s->get_buffer(s, pic);
  347. }
  348. /* If internal buffer type return the same buffer */
  349. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  350. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  351. else pic->pkt_pts= AV_NOPTS_VALUE;
  352. pic->reordered_opaque= s->reordered_opaque;
  353. return 0;
  354. }
  355. /*
  356. * Not internal type and reget_buffer not overridden, emulate cr buffer
  357. */
  358. temp_pic = *pic;
  359. for(i = 0; i < 4; i++)
  360. pic->data[i] = pic->base[i] = NULL;
  361. pic->opaque = NULL;
  362. /* Allocate new frame */
  363. if (s->get_buffer(s, pic))
  364. return -1;
  365. /* Copy image data from old buffer to new buffer */
  366. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  367. s->height);
  368. s->release_buffer(s, &temp_pic); // Release old frame
  369. return 0;
  370. }
  371. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  372. int i;
  373. for(i=0; i<count; i++){
  374. int r= func(c, (char*)arg + i*size);
  375. if(ret) ret[i]= r;
  376. }
  377. return 0;
  378. }
  379. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  380. int i;
  381. for(i=0; i<count; i++){
  382. int r= func(c, arg, i, 0);
  383. if(ret) ret[i]= r;
  384. }
  385. return 0;
  386. }
  387. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  388. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  389. ++fmt;
  390. return fmt[0];
  391. }
  392. void avcodec_get_frame_defaults(AVFrame *pic){
  393. memset(pic, 0, sizeof(AVFrame));
  394. pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  395. pic->key_frame= 1;
  396. }
  397. AVFrame *avcodec_alloc_frame(void){
  398. AVFrame *pic= av_malloc(sizeof(AVFrame));
  399. if(pic==NULL) return NULL;
  400. avcodec_get_frame_defaults(pic);
  401. return pic;
  402. }
  403. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  404. {
  405. int ret= -1;
  406. /* If there is a user-supplied mutex locking routine, call it. */
  407. if (ff_lockmgr_cb) {
  408. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  409. return -1;
  410. }
  411. entangled_thread_counter++;
  412. if(entangled_thread_counter != 1){
  413. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  414. goto end;
  415. }
  416. if(avctx->codec || !codec)
  417. goto end;
  418. if (codec->priv_data_size > 0) {
  419. if(!avctx->priv_data){
  420. avctx->priv_data = av_mallocz(codec->priv_data_size);
  421. if (!avctx->priv_data) {
  422. ret = AVERROR(ENOMEM);
  423. goto end;
  424. }
  425. if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
  426. *(AVClass**)avctx->priv_data= codec->priv_class;
  427. av_opt_set_defaults(avctx->priv_data);
  428. }
  429. }
  430. } else {
  431. avctx->priv_data = NULL;
  432. }
  433. if(avctx->coded_width && avctx->coded_height)
  434. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  435. else if(avctx->width && avctx->height)
  436. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  437. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  438. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  439. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  440. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  441. avcodec_set_dimensions(avctx, 0, 0);
  442. }
  443. /* if the decoder init function was already called previously,
  444. free the already allocated subtitle_header before overwriting it */
  445. if (codec->decode)
  446. av_freep(&avctx->subtitle_header);
  447. #define SANE_NB_CHANNELS 128U
  448. if (avctx->channels > SANE_NB_CHANNELS) {
  449. ret = AVERROR(EINVAL);
  450. goto free_and_end;
  451. }
  452. avctx->codec = codec;
  453. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  454. avctx->codec_id == CODEC_ID_NONE) {
  455. avctx->codec_type = codec->type;
  456. avctx->codec_id = codec->id;
  457. }
  458. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  459. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  460. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  461. goto free_and_end;
  462. }
  463. avctx->frame_number = 0;
  464. if (HAVE_THREADS && !avctx->thread_opaque) {
  465. ret = ff_thread_init(avctx);
  466. if (ret < 0) {
  467. goto free_and_end;
  468. }
  469. }
  470. if (avctx->codec->max_lowres < avctx->lowres) {
  471. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  472. avctx->codec->max_lowres);
  473. goto free_and_end;
  474. }
  475. if (avctx->codec->sample_fmts && avctx->codec->encode) {
  476. int i;
  477. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  478. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  479. break;
  480. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  481. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  482. goto free_and_end;
  483. }
  484. }
  485. avctx->pts_correction_num_faulty_pts =
  486. avctx->pts_correction_num_faulty_dts = 0;
  487. avctx->pts_correction_last_pts =
  488. avctx->pts_correction_last_dts = INT64_MIN;
  489. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  490. ret = avctx->codec->init(avctx);
  491. if (ret < 0) {
  492. goto free_and_end;
  493. }
  494. }
  495. ret=0;
  496. end:
  497. entangled_thread_counter--;
  498. /* Release any user-supplied mutex. */
  499. if (ff_lockmgr_cb) {
  500. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  501. }
  502. return ret;
  503. free_and_end:
  504. av_freep(&avctx->priv_data);
  505. avctx->codec= NULL;
  506. goto end;
  507. }
  508. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  509. const short *samples)
  510. {
  511. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  512. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  513. return -1;
  514. }
  515. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  516. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  517. avctx->frame_number++;
  518. return ret;
  519. }else
  520. return 0;
  521. }
  522. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  523. const AVFrame *pict)
  524. {
  525. if(buf_size < FF_MIN_BUFFER_SIZE){
  526. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  527. return -1;
  528. }
  529. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  530. return -1;
  531. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  532. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  533. avctx->frame_number++;
  534. emms_c(); //needed to avoid an emms_c() call before every return;
  535. return ret;
  536. }else
  537. return 0;
  538. }
  539. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  540. const AVSubtitle *sub)
  541. {
  542. int ret;
  543. if(sub->start_display_time) {
  544. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  545. return -1;
  546. }
  547. if(sub->num_rects == 0 || !sub->rects)
  548. return -1;
  549. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  550. avctx->frame_number++;
  551. return ret;
  552. }
  553. /**
  554. * Attempt to guess proper monotonic timestamps for decoded video frames
  555. * which might have incorrect times. Input timestamps may wrap around, in
  556. * which case the output will as well.
  557. *
  558. * @param pts the pts field of the decoded AVPacket, as passed through
  559. * AVFrame.pkt_pts
  560. * @param dts the dts field of the decoded AVPacket
  561. * @return one of the input values, may be AV_NOPTS_VALUE
  562. */
  563. static int64_t guess_correct_pts(AVCodecContext *ctx,
  564. int64_t reordered_pts, int64_t dts)
  565. {
  566. int64_t pts = AV_NOPTS_VALUE;
  567. if (dts != AV_NOPTS_VALUE) {
  568. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  569. ctx->pts_correction_last_dts = dts;
  570. }
  571. if (reordered_pts != AV_NOPTS_VALUE) {
  572. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  573. ctx->pts_correction_last_pts = reordered_pts;
  574. }
  575. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  576. && reordered_pts != AV_NOPTS_VALUE)
  577. pts = reordered_pts;
  578. else
  579. pts = dts;
  580. return pts;
  581. }
  582. #if FF_API_VIDEO_OLD
  583. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  584. int *got_picture_ptr,
  585. const uint8_t *buf, int buf_size)
  586. {
  587. AVPacket avpkt;
  588. av_init_packet(&avpkt);
  589. avpkt.data = buf;
  590. avpkt.size = buf_size;
  591. // HACK for CorePNG to decode as normal PNG by default
  592. avpkt.flags = AV_PKT_FLAG_KEY;
  593. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  594. }
  595. #endif
  596. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  597. int *got_picture_ptr,
  598. AVPacket *avpkt)
  599. {
  600. int ret;
  601. *got_picture_ptr= 0;
  602. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  603. return -1;
  604. avctx->pkt = avpkt;
  605. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  606. if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  607. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  608. avpkt);
  609. else {
  610. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  611. avpkt);
  612. picture->pkt_dts= avpkt->dts;
  613. }
  614. emms_c(); //needed to avoid an emms_c() call before every return;
  615. if (*got_picture_ptr){
  616. avctx->frame_number++;
  617. picture->best_effort_timestamp = guess_correct_pts(avctx,
  618. picture->pkt_pts,
  619. picture->pkt_dts);
  620. }
  621. }else
  622. ret= 0;
  623. return ret;
  624. }
  625. #if FF_API_AUDIO_OLD
  626. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  627. int *frame_size_ptr,
  628. const uint8_t *buf, int buf_size)
  629. {
  630. AVPacket avpkt;
  631. av_init_packet(&avpkt);
  632. avpkt.data = buf;
  633. avpkt.size = buf_size;
  634. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  635. }
  636. #endif
  637. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  638. int *frame_size_ptr,
  639. AVPacket *avpkt)
  640. {
  641. int ret;
  642. avctx->pkt = avpkt;
  643. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  644. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  645. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  646. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  647. return -1;
  648. }
  649. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  650. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  651. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  652. return -1;
  653. }
  654. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  655. avctx->frame_number++;
  656. }else{
  657. ret= 0;
  658. *frame_size_ptr=0;
  659. }
  660. return ret;
  661. }
  662. #if FF_API_SUBTITLE_OLD
  663. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  664. int *got_sub_ptr,
  665. const uint8_t *buf, int buf_size)
  666. {
  667. AVPacket avpkt;
  668. av_init_packet(&avpkt);
  669. avpkt.data = buf;
  670. avpkt.size = buf_size;
  671. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  672. }
  673. #endif
  674. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  675. int *got_sub_ptr,
  676. AVPacket *avpkt)
  677. {
  678. int ret;
  679. avctx->pkt = avpkt;
  680. *got_sub_ptr = 0;
  681. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  682. if (*got_sub_ptr)
  683. avctx->frame_number++;
  684. return ret;
  685. }
  686. void avsubtitle_free(AVSubtitle *sub)
  687. {
  688. int i;
  689. for (i = 0; i < sub->num_rects; i++)
  690. {
  691. av_freep(&sub->rects[i]->pict.data[0]);
  692. av_freep(&sub->rects[i]->pict.data[1]);
  693. av_freep(&sub->rects[i]->pict.data[2]);
  694. av_freep(&sub->rects[i]->pict.data[3]);
  695. av_freep(&sub->rects[i]->text);
  696. av_freep(&sub->rects[i]->ass);
  697. av_freep(&sub->rects[i]);
  698. }
  699. av_freep(&sub->rects);
  700. memset(sub, 0, sizeof(AVSubtitle));
  701. }
  702. av_cold int avcodec_close(AVCodecContext *avctx)
  703. {
  704. /* If there is a user-supplied mutex locking routine, call it. */
  705. if (ff_lockmgr_cb) {
  706. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  707. return -1;
  708. }
  709. entangled_thread_counter++;
  710. if(entangled_thread_counter != 1){
  711. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  712. entangled_thread_counter--;
  713. return -1;
  714. }
  715. if (HAVE_THREADS && avctx->thread_opaque)
  716. ff_thread_free(avctx);
  717. if (avctx->codec && avctx->codec->close)
  718. avctx->codec->close(avctx);
  719. avcodec_default_free_buffers(avctx);
  720. avctx->coded_frame = NULL;
  721. av_freep(&avctx->priv_data);
  722. if(avctx->codec && avctx->codec->encode)
  723. av_freep(&avctx->extradata);
  724. avctx->codec = NULL;
  725. avctx->active_thread_type = 0;
  726. entangled_thread_counter--;
  727. /* Release any user-supplied mutex. */
  728. if (ff_lockmgr_cb) {
  729. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  730. }
  731. return 0;
  732. }
  733. AVCodec *avcodec_find_encoder(enum CodecID id)
  734. {
  735. AVCodec *p, *experimental=NULL;
  736. p = first_avcodec;
  737. while (p) {
  738. if (p->encode != NULL && p->id == id) {
  739. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  740. experimental = p;
  741. } else
  742. return p;
  743. }
  744. p = p->next;
  745. }
  746. return experimental;
  747. }
  748. AVCodec *avcodec_find_encoder_by_name(const char *name)
  749. {
  750. AVCodec *p;
  751. if (!name)
  752. return NULL;
  753. p = first_avcodec;
  754. while (p) {
  755. if (p->encode != NULL && strcmp(name,p->name) == 0)
  756. return p;
  757. p = p->next;
  758. }
  759. return NULL;
  760. }
  761. AVCodec *avcodec_find_decoder(enum CodecID id)
  762. {
  763. AVCodec *p;
  764. p = first_avcodec;
  765. while (p) {
  766. if (p->decode != NULL && p->id == id)
  767. return p;
  768. p = p->next;
  769. }
  770. return NULL;
  771. }
  772. AVCodec *avcodec_find_decoder_by_name(const char *name)
  773. {
  774. AVCodec *p;
  775. if (!name)
  776. return NULL;
  777. p = first_avcodec;
  778. while (p) {
  779. if (p->decode != NULL && strcmp(name,p->name) == 0)
  780. return p;
  781. p = p->next;
  782. }
  783. return NULL;
  784. }
  785. static int get_bit_rate(AVCodecContext *ctx)
  786. {
  787. int bit_rate;
  788. int bits_per_sample;
  789. switch(ctx->codec_type) {
  790. case AVMEDIA_TYPE_VIDEO:
  791. case AVMEDIA_TYPE_DATA:
  792. case AVMEDIA_TYPE_SUBTITLE:
  793. case AVMEDIA_TYPE_ATTACHMENT:
  794. bit_rate = ctx->bit_rate;
  795. break;
  796. case AVMEDIA_TYPE_AUDIO:
  797. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  798. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  799. break;
  800. default:
  801. bit_rate = 0;
  802. break;
  803. }
  804. return bit_rate;
  805. }
  806. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  807. {
  808. int i, len, ret = 0;
  809. for (i = 0; i < 4; i++) {
  810. len = snprintf(buf, buf_size,
  811. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  812. buf += len;
  813. buf_size = buf_size > len ? buf_size - len : 0;
  814. ret += len;
  815. codec_tag>>=8;
  816. }
  817. return ret;
  818. }
  819. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  820. {
  821. const char *codec_name;
  822. const char *profile = NULL;
  823. AVCodec *p;
  824. char buf1[32];
  825. int bitrate;
  826. AVRational display_aspect_ratio;
  827. if (encode)
  828. p = avcodec_find_encoder(enc->codec_id);
  829. else
  830. p = avcodec_find_decoder(enc->codec_id);
  831. if (p) {
  832. codec_name = p->name;
  833. profile = av_get_profile_name(p, enc->profile);
  834. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  835. /* fake mpeg2 transport stream codec (currently not
  836. registered) */
  837. codec_name = "mpeg2ts";
  838. } else if (enc->codec_name[0] != '\0') {
  839. codec_name = enc->codec_name;
  840. } else {
  841. /* output avi tags */
  842. char tag_buf[32];
  843. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  844. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  845. codec_name = buf1;
  846. }
  847. switch(enc->codec_type) {
  848. case AVMEDIA_TYPE_VIDEO:
  849. snprintf(buf, buf_size,
  850. "Video: %s%s",
  851. codec_name, enc->mb_decision ? " (hq)" : "");
  852. if (profile)
  853. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  854. " (%s)", profile);
  855. if (enc->pix_fmt != PIX_FMT_NONE) {
  856. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  857. ", %s",
  858. avcodec_get_pix_fmt_name(enc->pix_fmt));
  859. }
  860. if (enc->width) {
  861. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  862. ", %dx%d",
  863. enc->width, enc->height);
  864. if (enc->sample_aspect_ratio.num) {
  865. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  866. enc->width*enc->sample_aspect_ratio.num,
  867. enc->height*enc->sample_aspect_ratio.den,
  868. 1024*1024);
  869. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  870. " [PAR %d:%d DAR %d:%d]",
  871. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  872. display_aspect_ratio.num, display_aspect_ratio.den);
  873. }
  874. if(av_log_get_level() >= AV_LOG_DEBUG){
  875. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  876. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  877. ", %d/%d",
  878. enc->time_base.num/g, enc->time_base.den/g);
  879. }
  880. }
  881. if (encode) {
  882. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  883. ", q=%d-%d", enc->qmin, enc->qmax);
  884. }
  885. break;
  886. case AVMEDIA_TYPE_AUDIO:
  887. snprintf(buf, buf_size,
  888. "Audio: %s",
  889. codec_name);
  890. if (profile)
  891. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  892. " (%s)", profile);
  893. if (enc->sample_rate) {
  894. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  895. ", %d Hz", enc->sample_rate);
  896. }
  897. av_strlcat(buf, ", ", buf_size);
  898. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  899. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  900. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  901. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  902. }
  903. break;
  904. case AVMEDIA_TYPE_DATA:
  905. snprintf(buf, buf_size, "Data: %s", codec_name);
  906. break;
  907. case AVMEDIA_TYPE_SUBTITLE:
  908. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  909. break;
  910. case AVMEDIA_TYPE_ATTACHMENT:
  911. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  912. break;
  913. default:
  914. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  915. return;
  916. }
  917. if (encode) {
  918. if (enc->flags & CODEC_FLAG_PASS1)
  919. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  920. ", pass 1");
  921. if (enc->flags & CODEC_FLAG_PASS2)
  922. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  923. ", pass 2");
  924. }
  925. bitrate = get_bit_rate(enc);
  926. if (bitrate != 0) {
  927. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  928. ", %d kb/s", bitrate / 1000);
  929. }
  930. }
  931. const char *av_get_profile_name(const AVCodec *codec, int profile)
  932. {
  933. const AVProfile *p;
  934. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  935. return NULL;
  936. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  937. if (p->profile == profile)
  938. return p->name;
  939. return NULL;
  940. }
  941. unsigned avcodec_version( void )
  942. {
  943. return LIBAVCODEC_VERSION_INT;
  944. }
  945. const char *avcodec_configuration(void)
  946. {
  947. return FFMPEG_CONFIGURATION;
  948. }
  949. const char *avcodec_license(void)
  950. {
  951. #define LICENSE_PREFIX "libavcodec license: "
  952. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  953. }
  954. void avcodec_init(void)
  955. {
  956. static int initialized = 0;
  957. if (initialized != 0)
  958. return;
  959. initialized = 1;
  960. dsputil_static_init();
  961. }
  962. void avcodec_flush_buffers(AVCodecContext *avctx)
  963. {
  964. if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  965. ff_thread_flush(avctx);
  966. if(avctx->codec->flush)
  967. avctx->codec->flush(avctx);
  968. }
  969. void avcodec_default_free_buffers(AVCodecContext *s){
  970. int i, j;
  971. if(s->internal_buffer==NULL) return;
  972. if (s->internal_buffer_count)
  973. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  974. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  975. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  976. for(j=0; j<4; j++){
  977. av_freep(&buf->base[j]);
  978. buf->data[j]= NULL;
  979. }
  980. }
  981. av_freep(&s->internal_buffer);
  982. s->internal_buffer_count=0;
  983. }
  984. char av_get_pict_type_char(int pict_type){
  985. switch(pict_type){
  986. case FF_I_TYPE: return 'I';
  987. case FF_P_TYPE: return 'P';
  988. case FF_B_TYPE: return 'B';
  989. case FF_S_TYPE: return 'S';
  990. case FF_SI_TYPE:return 'i';
  991. case FF_SP_TYPE:return 'p';
  992. case FF_BI_TYPE:return 'b';
  993. default: return '?';
  994. }
  995. }
  996. int av_get_bits_per_sample(enum CodecID codec_id){
  997. switch(codec_id){
  998. case CODEC_ID_ADPCM_SBPRO_2:
  999. return 2;
  1000. case CODEC_ID_ADPCM_SBPRO_3:
  1001. return 3;
  1002. case CODEC_ID_ADPCM_SBPRO_4:
  1003. case CODEC_ID_ADPCM_CT:
  1004. case CODEC_ID_ADPCM_IMA_WAV:
  1005. case CODEC_ID_ADPCM_MS:
  1006. case CODEC_ID_ADPCM_YAMAHA:
  1007. return 4;
  1008. case CODEC_ID_PCM_ALAW:
  1009. case CODEC_ID_PCM_MULAW:
  1010. case CODEC_ID_PCM_S8:
  1011. case CODEC_ID_PCM_U8:
  1012. case CODEC_ID_PCM_ZORK:
  1013. return 8;
  1014. case CODEC_ID_PCM_S16BE:
  1015. case CODEC_ID_PCM_S16LE:
  1016. case CODEC_ID_PCM_S16LE_PLANAR:
  1017. case CODEC_ID_PCM_U16BE:
  1018. case CODEC_ID_PCM_U16LE:
  1019. return 16;
  1020. case CODEC_ID_PCM_S24DAUD:
  1021. case CODEC_ID_PCM_S24BE:
  1022. case CODEC_ID_PCM_S24LE:
  1023. case CODEC_ID_PCM_U24BE:
  1024. case CODEC_ID_PCM_U24LE:
  1025. return 24;
  1026. case CODEC_ID_PCM_S32BE:
  1027. case CODEC_ID_PCM_S32LE:
  1028. case CODEC_ID_PCM_U32BE:
  1029. case CODEC_ID_PCM_U32LE:
  1030. case CODEC_ID_PCM_F32BE:
  1031. case CODEC_ID_PCM_F32LE:
  1032. return 32;
  1033. case CODEC_ID_PCM_F64BE:
  1034. case CODEC_ID_PCM_F64LE:
  1035. return 64;
  1036. default:
  1037. return 0;
  1038. }
  1039. }
  1040. #if FF_API_OLD_SAMPLE_FMT
  1041. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1042. return av_get_bits_per_sample_fmt(sample_fmt);
  1043. }
  1044. #endif
  1045. #if !HAVE_THREADS
  1046. int ff_thread_init(AVCodecContext *s){
  1047. return -1;
  1048. }
  1049. #endif
  1050. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1051. {
  1052. unsigned int n = 0;
  1053. while(v >= 0xff) {
  1054. *s++ = 0xff;
  1055. v -= 0xff;
  1056. n++;
  1057. }
  1058. *s = v;
  1059. n++;
  1060. return n;
  1061. }
  1062. #if LIBAVCODEC_VERSION_MAJOR < 53
  1063. #include "libavutil/parseutils.h"
  1064. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  1065. {
  1066. return av_parse_video_size(width_ptr, height_ptr, str);
  1067. }
  1068. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  1069. {
  1070. return av_parse_video_rate(frame_rate, arg);
  1071. }
  1072. #endif
  1073. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1074. int i;
  1075. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1076. return i;
  1077. }
  1078. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1079. {
  1080. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1081. "version to the newest one from Git. If the problem still "
  1082. "occurs, it means that your file has a feature which has not "
  1083. "been implemented.", feature);
  1084. if(want_sample)
  1085. av_log_ask_for_sample(avc, NULL);
  1086. else
  1087. av_log(avc, AV_LOG_WARNING, "\n");
  1088. }
  1089. void av_log_ask_for_sample(void *avc, const char *msg)
  1090. {
  1091. if (msg)
  1092. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  1093. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1094. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1095. "and contact the ffmpeg-devel mailing list.\n");
  1096. }
  1097. static AVHWAccel *first_hwaccel = NULL;
  1098. void av_register_hwaccel(AVHWAccel *hwaccel)
  1099. {
  1100. AVHWAccel **p = &first_hwaccel;
  1101. while (*p)
  1102. p = &(*p)->next;
  1103. *p = hwaccel;
  1104. hwaccel->next = NULL;
  1105. }
  1106. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1107. {
  1108. return hwaccel ? hwaccel->next : first_hwaccel;
  1109. }
  1110. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1111. {
  1112. AVHWAccel *hwaccel=NULL;
  1113. while((hwaccel= av_hwaccel_next(hwaccel))){
  1114. if ( hwaccel->id == codec_id
  1115. && hwaccel->pix_fmt == pix_fmt)
  1116. return hwaccel;
  1117. }
  1118. return NULL;
  1119. }
  1120. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1121. {
  1122. if (ff_lockmgr_cb) {
  1123. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1124. return -1;
  1125. }
  1126. ff_lockmgr_cb = cb;
  1127. if (ff_lockmgr_cb) {
  1128. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1129. return -1;
  1130. }
  1131. return 0;
  1132. }
  1133. unsigned int ff_toupper4(unsigned int x)
  1134. {
  1135. return toupper( x &0xFF)
  1136. + (toupper((x>>8 )&0xFF)<<8 )
  1137. + (toupper((x>>16)&0xFF)<<16)
  1138. + (toupper((x>>24)&0xFF)<<24);
  1139. }
  1140. #if !HAVE_PTHREADS
  1141. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1142. {
  1143. f->owner = avctx;
  1144. return avctx->get_buffer(avctx, f);
  1145. }
  1146. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1147. {
  1148. f->owner->release_buffer(f->owner, f);
  1149. }
  1150. void ff_thread_finish_setup(AVCodecContext *avctx)
  1151. {
  1152. }
  1153. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1154. {
  1155. }
  1156. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1157. {
  1158. }
  1159. #endif
  1160. #if LIBAVCODEC_VERSION_MAJOR < 53
  1161. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1162. {
  1163. s->thread_count = thread_count;
  1164. s->thread_type = FF_THREAD_FRAME | FF_THREAD_SLICE;
  1165. return ff_thread_init(s);
  1166. }
  1167. void avcodec_thread_free(AVCodecContext *s)
  1168. {
  1169. #if HAVE_THREADS
  1170. ff_thread_free(s);
  1171. #endif
  1172. }
  1173. #endif