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.

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