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.

1188 lines
34KB

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