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.

549 lines
14KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(unsigned int size)
  23. {
  24. void *ptr;
  25. ptr = av_malloc(size);
  26. if (!ptr)
  27. return NULL;
  28. memset(ptr, 0, size);
  29. return ptr;
  30. }
  31. /* allocation of static arrays - do not use for normal allocation */
  32. static unsigned int last_static = 0;
  33. static char*** array_static = NULL;
  34. static const unsigned int grow_static = 64; // ^2
  35. void *__av_mallocz_static(void** location, unsigned int size)
  36. {
  37. int l = (last_static + grow_static) & ~(grow_static - 1);
  38. void *ptr = av_mallocz(size);
  39. if (!ptr)
  40. return NULL;
  41. if (location)
  42. {
  43. if (l > last_static)
  44. array_static = realloc(array_static, l);
  45. array_static[last_static++] = (char**) location;
  46. *location = ptr;
  47. }
  48. return ptr;
  49. }
  50. /* free all static arrays and reset pointers to 0 */
  51. void av_free_static()
  52. {
  53. if (array_static)
  54. {
  55. unsigned i;
  56. for (i = 0; i < last_static; i++)
  57. {
  58. free(*array_static[i]);
  59. *array_static[i] = NULL;
  60. }
  61. free(array_static);
  62. array_static = 0;
  63. }
  64. last_static = 0;
  65. }
  66. /* cannot call it directly because of 'void **' casting is not automatic */
  67. void __av_freep(void **ptr)
  68. {
  69. av_free(*ptr);
  70. *ptr = NULL;
  71. }
  72. /* encoder management */
  73. AVCodec *first_avcodec;
  74. void register_avcodec(AVCodec *format)
  75. {
  76. AVCodec **p;
  77. p = &first_avcodec;
  78. while (*p != NULL) p = &(*p)->next;
  79. *p = format;
  80. format->next = NULL;
  81. }
  82. void avcodec_get_context_defaults(AVCodecContext *s){
  83. s->bit_rate= 800*1000;
  84. s->bit_rate_tolerance= s->bit_rate*10;
  85. s->qmin= 2;
  86. s->qmax= 31;
  87. s->rc_eq= "tex^qComp";
  88. s->qcompress= 0.5;
  89. s->max_qdiff= 3;
  90. s->b_quant_factor=1.25;
  91. s->b_quant_offset=1.25;
  92. s->i_quant_factor=-0.8;
  93. s->i_quant_offset=0.0;
  94. s->error_concealment= 3;
  95. s->error_resilience= 1;
  96. s->workaround_bugs= FF_BUG_AUTODETECT;
  97. s->frame_rate = 25 * FRAME_RATE_BASE;
  98. s->gop_size= 50;
  99. s->me_method= ME_EPZS;
  100. }
  101. /**
  102. * allocates a AVCodecContext and set it to defaults.
  103. * this can be deallocated by simply calling free()
  104. */
  105. AVCodecContext *avcodec_alloc_context(void){
  106. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  107. if(avctx==NULL) return NULL;
  108. avcodec_get_context_defaults(avctx);
  109. return avctx;
  110. }
  111. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  112. {
  113. int ret;
  114. avctx->codec = codec;
  115. avctx->frame_number = 0;
  116. if (codec->priv_data_size > 0) {
  117. avctx->priv_data = av_mallocz(codec->priv_data_size);
  118. if (!avctx->priv_data)
  119. return -ENOMEM;
  120. } else {
  121. avctx->priv_data = NULL;
  122. }
  123. ret = avctx->codec->init(avctx);
  124. if (ret < 0) {
  125. av_freep(&avctx->priv_data);
  126. return ret;
  127. }
  128. return 0;
  129. }
  130. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  131. const short *samples)
  132. {
  133. int ret;
  134. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  135. avctx->frame_number++;
  136. return ret;
  137. }
  138. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  139. const AVPicture *pict)
  140. {
  141. int ret;
  142. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  143. emms_c(); //needed to avoid a emms_c() call before every return;
  144. avctx->frame_number++;
  145. return ret;
  146. }
  147. /* decode a frame. return -1 if error, otherwise return the number of
  148. bytes used. If no frame could be decompressed, *got_picture_ptr is
  149. zero. Otherwise, it is non zero */
  150. int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
  151. int *got_picture_ptr,
  152. UINT8 *buf, int buf_size)
  153. {
  154. int ret;
  155. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  156. buf, buf_size);
  157. emms_c(); //needed to avoid a emms_c() call before every return;
  158. if (*got_picture_ptr)
  159. avctx->frame_number++;
  160. return ret;
  161. }
  162. /* decode an audio frame. return -1 if error, otherwise return the
  163. *number of bytes used. If no frame could be decompressed,
  164. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  165. *size in BYTES. */
  166. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  167. int *frame_size_ptr,
  168. UINT8 *buf, int buf_size)
  169. {
  170. int ret;
  171. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  172. buf, buf_size);
  173. avctx->frame_number++;
  174. return ret;
  175. }
  176. int avcodec_close(AVCodecContext *avctx)
  177. {
  178. if (avctx->codec->close)
  179. avctx->codec->close(avctx);
  180. av_freep(&avctx->priv_data);
  181. avctx->codec = NULL;
  182. return 0;
  183. }
  184. AVCodec *avcodec_find_encoder(enum CodecID id)
  185. {
  186. AVCodec *p;
  187. p = first_avcodec;
  188. while (p) {
  189. if (p->encode != NULL && p->id == id)
  190. return p;
  191. p = p->next;
  192. }
  193. return NULL;
  194. }
  195. AVCodec *avcodec_find_encoder_by_name(const char *name)
  196. {
  197. AVCodec *p;
  198. p = first_avcodec;
  199. while (p) {
  200. if (p->encode != NULL && strcmp(name,p->name) == 0)
  201. return p;
  202. p = p->next;
  203. }
  204. return NULL;
  205. }
  206. AVCodec *avcodec_find_decoder(enum CodecID id)
  207. {
  208. AVCodec *p;
  209. p = first_avcodec;
  210. while (p) {
  211. if (p->decode != NULL && p->id == id)
  212. return p;
  213. p = p->next;
  214. }
  215. return NULL;
  216. }
  217. AVCodec *avcodec_find_decoder_by_name(const char *name)
  218. {
  219. AVCodec *p;
  220. p = first_avcodec;
  221. while (p) {
  222. if (p->decode != NULL && strcmp(name,p->name) == 0)
  223. return p;
  224. p = p->next;
  225. }
  226. return NULL;
  227. }
  228. AVCodec *avcodec_find(enum CodecID id)
  229. {
  230. AVCodec *p;
  231. p = first_avcodec;
  232. while (p) {
  233. if (p->id == id)
  234. return p;
  235. p = p->next;
  236. }
  237. return NULL;
  238. }
  239. const char *pix_fmt_str[] = {
  240. "yuv420p",
  241. "yuv422",
  242. "rgb24",
  243. "bgr24",
  244. "yuv422p",
  245. "yuv444p",
  246. "rgba32",
  247. "bgra32",
  248. "yuv410p",
  249. "yuv411p",
  250. };
  251. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  252. {
  253. const char *codec_name;
  254. AVCodec *p;
  255. char buf1[32];
  256. char channels_str[100];
  257. int bitrate;
  258. if (encode)
  259. p = avcodec_find_encoder(enc->codec_id);
  260. else
  261. p = avcodec_find_decoder(enc->codec_id);
  262. if (p) {
  263. codec_name = p->name;
  264. } else if (enc->codec_name[0] != '\0') {
  265. codec_name = enc->codec_name;
  266. } else {
  267. /* output avi tags */
  268. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  269. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  270. enc->codec_tag & 0xff,
  271. (enc->codec_tag >> 8) & 0xff,
  272. (enc->codec_tag >> 16) & 0xff,
  273. (enc->codec_tag >> 24) & 0xff);
  274. } else {
  275. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  276. }
  277. codec_name = buf1;
  278. }
  279. switch(enc->codec_type) {
  280. case CODEC_TYPE_VIDEO:
  281. snprintf(buf, buf_size,
  282. "Video: %s%s",
  283. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  284. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  285. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  286. ", %s",
  287. pix_fmt_str[enc->pix_fmt]);
  288. }
  289. if (enc->width) {
  290. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  291. ", %dx%d, %0.2f fps",
  292. enc->width, enc->height,
  293. (float)enc->frame_rate / FRAME_RATE_BASE);
  294. }
  295. if (encode) {
  296. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  297. ", q=%d-%d", enc->qmin, enc->qmax);
  298. }
  299. bitrate = enc->bit_rate;
  300. break;
  301. case CODEC_TYPE_AUDIO:
  302. snprintf(buf, buf_size,
  303. "Audio: %s",
  304. codec_name);
  305. switch (enc->channels) {
  306. case 1:
  307. strcpy(channels_str, "mono");
  308. break;
  309. case 2:
  310. strcpy(channels_str, "stereo");
  311. break;
  312. case 6:
  313. strcpy(channels_str, "5:1");
  314. break;
  315. default:
  316. sprintf(channels_str, "%d channels", enc->channels);
  317. break;
  318. }
  319. if (enc->sample_rate) {
  320. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  321. ", %d Hz, %s",
  322. enc->sample_rate,
  323. channels_str);
  324. }
  325. /* for PCM codecs, compute bitrate directly */
  326. switch(enc->codec_id) {
  327. case CODEC_ID_PCM_S16LE:
  328. case CODEC_ID_PCM_S16BE:
  329. case CODEC_ID_PCM_U16LE:
  330. case CODEC_ID_PCM_U16BE:
  331. bitrate = enc->sample_rate * enc->channels * 16;
  332. break;
  333. case CODEC_ID_PCM_S8:
  334. case CODEC_ID_PCM_U8:
  335. case CODEC_ID_PCM_ALAW:
  336. case CODEC_ID_PCM_MULAW:
  337. bitrate = enc->sample_rate * enc->channels * 8;
  338. break;
  339. default:
  340. bitrate = enc->bit_rate;
  341. break;
  342. }
  343. break;
  344. default:
  345. av_abort();
  346. }
  347. if (encode) {
  348. if (enc->flags & CODEC_FLAG_PASS1)
  349. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  350. ", pass 1");
  351. if (enc->flags & CODEC_FLAG_PASS2)
  352. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  353. ", pass 2");
  354. }
  355. if (bitrate != 0) {
  356. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  357. ", %d kb/s", bitrate / 1000);
  358. }
  359. }
  360. /* Picture field are filled with 'ptr' addresses */
  361. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  362. int pix_fmt, int width, int height)
  363. {
  364. int size;
  365. size = width * height;
  366. switch(pix_fmt) {
  367. case PIX_FMT_YUV420P:
  368. picture->data[0] = ptr;
  369. picture->data[1] = picture->data[0] + size;
  370. picture->data[2] = picture->data[1] + size / 4;
  371. picture->linesize[0] = width;
  372. picture->linesize[1] = width / 2;
  373. picture->linesize[2] = width / 2;
  374. break;
  375. case PIX_FMT_YUV422P:
  376. picture->data[0] = ptr;
  377. picture->data[1] = picture->data[0] + size;
  378. picture->data[2] = picture->data[1] + size / 2;
  379. picture->linesize[0] = width;
  380. picture->linesize[1] = width / 2;
  381. picture->linesize[2] = width / 2;
  382. break;
  383. case PIX_FMT_YUV444P:
  384. picture->data[0] = ptr;
  385. picture->data[1] = picture->data[0] + size;
  386. picture->data[2] = picture->data[1] + size;
  387. picture->linesize[0] = width;
  388. picture->linesize[1] = width;
  389. picture->linesize[2] = width;
  390. break;
  391. case PIX_FMT_RGB24:
  392. case PIX_FMT_BGR24:
  393. picture->data[0] = ptr;
  394. picture->data[1] = NULL;
  395. picture->data[2] = NULL;
  396. picture->linesize[0] = width * 3;
  397. break;
  398. case PIX_FMT_RGBA32:
  399. case PIX_FMT_BGRA32:
  400. picture->data[0] = ptr;
  401. picture->data[1] = NULL;
  402. picture->data[2] = NULL;
  403. picture->linesize[0] = width * 4;
  404. break;
  405. case PIX_FMT_YUV422:
  406. picture->data[0] = ptr;
  407. picture->data[1] = NULL;
  408. picture->data[2] = NULL;
  409. picture->linesize[0] = width * 2;
  410. break;
  411. default:
  412. picture->data[0] = NULL;
  413. picture->data[1] = NULL;
  414. picture->data[2] = NULL;
  415. break;
  416. }
  417. }
  418. int avpicture_get_size(int pix_fmt, int width, int height)
  419. {
  420. int size;
  421. size = width * height;
  422. switch(pix_fmt) {
  423. case PIX_FMT_YUV420P:
  424. size = (size * 3) / 2;
  425. break;
  426. case PIX_FMT_YUV422P:
  427. size = (size * 2);
  428. break;
  429. case PIX_FMT_YUV444P:
  430. size = (size * 3);
  431. break;
  432. case PIX_FMT_RGB24:
  433. case PIX_FMT_BGR24:
  434. size = (size * 3);
  435. break;
  436. case PIX_FMT_RGBA32:
  437. case PIX_FMT_BGRA32:
  438. size = (size * 4);
  439. break;
  440. case PIX_FMT_YUV422:
  441. size = (size * 2);
  442. break;
  443. default:
  444. size = -1;
  445. break;
  446. }
  447. return size;
  448. }
  449. unsigned avcodec_version( void )
  450. {
  451. return LIBAVCODEC_VERSION_INT;
  452. }
  453. unsigned avcodec_build( void )
  454. {
  455. return LIBAVCODEC_BUILD;
  456. }
  457. /* must be called before any other functions */
  458. void avcodec_init(void)
  459. {
  460. static int inited = 0;
  461. if (inited != 0)
  462. return;
  463. inited = 1;
  464. //dsputil_init();
  465. }
  466. /* this should be called after seeking and before trying to decode the next frame */
  467. void avcodec_flush_buffers(AVCodecContext *avctx)
  468. {
  469. MpegEncContext *s = avctx->priv_data;
  470. s->num_available_buffers=0;
  471. }
  472. static int raw_encode_init(AVCodecContext *s)
  473. {
  474. return 0;
  475. }
  476. static int raw_decode_frame(AVCodecContext *avctx,
  477. void *data, int *data_size,
  478. UINT8 *buf, int buf_size)
  479. {
  480. return -1;
  481. }
  482. static int raw_encode_frame(AVCodecContext *avctx,
  483. unsigned char *frame, int buf_size, void *data)
  484. {
  485. return -1;
  486. }
  487. AVCodec rawvideo_codec = {
  488. "rawvideo",
  489. CODEC_TYPE_VIDEO,
  490. CODEC_ID_RAWVIDEO,
  491. 0,
  492. raw_encode_init,
  493. raw_encode_frame,
  494. NULL,
  495. raw_decode_frame,
  496. };