PageRenderTime 2971ms CodeModel.GetById 33ms RepoModel.GetById 6ms app.codeStats 0ms

/lib/ffmpeg/libavformat/yuv4mpeg.c

https://github.com/hgmeier/xbmc
C | 402 lines | 329 code | 44 blank | 29 comment | 90 complexity | 12cb48e4cd3926d327ac64cc1dee4c19 MD5 | raw file
  1. /*
  2. * YUV4MPEG format
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #define Y4M_MAGIC "YUV4MPEG2"
  23. #define Y4M_FRAME_MAGIC "FRAME"
  24. #define Y4M_LINE_MAX 256
  25. struct frame_attributes {
  26. int interlaced_frame;
  27. int top_field_first;
  28. };
  29. #if CONFIG_YUV4MPEGPIPE_MUXER
  30. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  31. {
  32. AVStream *st;
  33. int width, height;
  34. int raten, rated, aspectn, aspectd, n;
  35. char inter;
  36. const char *colorspace = "";
  37. st = s->streams[0];
  38. width = st->codec->width;
  39. height = st->codec->height;
  40. av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
  41. aspectn = st->sample_aspect_ratio.num;
  42. aspectd = st->sample_aspect_ratio.den;
  43. if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
  44. inter = 'p'; /* progressive is the default */
  45. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
  46. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  47. }
  48. switch(st->codec->pix_fmt) {
  49. case PIX_FMT_GRAY8:
  50. colorspace = " Cmono";
  51. break;
  52. case PIX_FMT_YUV411P:
  53. colorspace = " C411 XYSCSS=411";
  54. break;
  55. case PIX_FMT_YUV420P:
  56. colorspace = (st->codec->chroma_sample_location == AVCHROMA_LOC_TOPLEFT)?" C420paldv XYSCSS=420PALDV":
  57. (st->codec->chroma_sample_location == AVCHROMA_LOC_LEFT) ?" C420mpeg2 XYSCSS=420MPEG2":
  58. " C420jpeg XYSCSS=420JPEG";
  59. break;
  60. case PIX_FMT_YUV422P:
  61. colorspace = " C422 XYSCSS=422";
  62. break;
  63. case PIX_FMT_YUV444P:
  64. colorspace = " C444 XYSCSS=444";
  65. break;
  66. }
  67. /* construct stream header, if this is the first frame */
  68. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  69. Y4M_MAGIC,
  70. width,
  71. height,
  72. raten, rated,
  73. inter,
  74. aspectn, aspectd,
  75. colorspace);
  76. return n;
  77. }
  78. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  79. {
  80. AVStream *st = s->streams[pkt->stream_index];
  81. ByteIOContext *pb = s->pb;
  82. AVPicture *picture;
  83. int* first_pkt = s->priv_data;
  84. int width, height, h_chroma_shift, v_chroma_shift;
  85. int i, m;
  86. char buf2[Y4M_LINE_MAX+1];
  87. char buf1[20];
  88. uint8_t *ptr, *ptr1, *ptr2;
  89. picture = (AVPicture *)pkt->data;
  90. /* for the first packet we have to output the header as well */
  91. if (*first_pkt) {
  92. *first_pkt = 0;
  93. if (yuv4_generate_header(s, buf2) < 0) {
  94. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  95. return AVERROR(EIO);
  96. } else {
  97. put_buffer(pb, buf2, strlen(buf2));
  98. }
  99. }
  100. /* construct frame header */
  101. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  102. put_buffer(pb, buf1, strlen(buf1));
  103. width = st->codec->width;
  104. height = st->codec->height;
  105. ptr = picture->data[0];
  106. for(i=0;i<height;i++) {
  107. put_buffer(pb, ptr, width);
  108. ptr += picture->linesize[0];
  109. }
  110. if (st->codec->pix_fmt != PIX_FMT_GRAY8){
  111. // Adjust for smaller Cb and Cr planes
  112. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  113. width >>= h_chroma_shift;
  114. height >>= v_chroma_shift;
  115. ptr1 = picture->data[1];
  116. ptr2 = picture->data[2];
  117. for(i=0;i<height;i++) { /* Cb */
  118. put_buffer(pb, ptr1, width);
  119. ptr1 += picture->linesize[1];
  120. }
  121. for(i=0;i<height;i++) { /* Cr */
  122. put_buffer(pb, ptr2, width);
  123. ptr2 += picture->linesize[2];
  124. }
  125. }
  126. put_flush_packet(pb);
  127. return 0;
  128. }
  129. static int yuv4_write_header(AVFormatContext *s)
  130. {
  131. int* first_pkt = s->priv_data;
  132. if (s->nb_streams != 1)
  133. return AVERROR(EIO);
  134. if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
  135. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
  136. }
  137. else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
  138. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
  139. (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
  140. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
  141. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
  142. return AVERROR(EIO);
  143. }
  144. *first_pkt = 1;
  145. return 0;
  146. }
  147. AVOutputFormat ff_yuv4mpegpipe_muxer = {
  148. "yuv4mpegpipe",
  149. NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  150. "",
  151. "y4m",
  152. sizeof(int),
  153. CODEC_ID_NONE,
  154. CODEC_ID_RAWVIDEO,
  155. yuv4_write_header,
  156. yuv4_write_packet,
  157. .flags = AVFMT_RAWPICTURE,
  158. };
  159. #endif
  160. /* Header size increased to allow room for optional flags */
  161. #define MAX_YUV4_HEADER 80
  162. #define MAX_FRAME_HEADER 80
  163. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  164. {
  165. char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
  166. char *tokstart,*tokend,*header_end;
  167. int i;
  168. ByteIOContext *pb = s->pb;
  169. int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
  170. enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
  171. enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
  172. AVStream *st;
  173. struct frame_attributes *s1 = s->priv_data;
  174. for (i=0; i<MAX_YUV4_HEADER; i++) {
  175. header[i] = get_byte(pb);
  176. if (header[i] == '\n') {
  177. header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
  178. header[i+2] = 0;
  179. break;
  180. }
  181. }
  182. if (i == MAX_YUV4_HEADER) return -1;
  183. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  184. s1->interlaced_frame = 0;
  185. s1->top_field_first = 0;
  186. header_end = &header[i+1]; // Include space
  187. for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
  188. if (*tokstart==0x20) continue;
  189. switch (*tokstart++) {
  190. case 'W': // Width. Required.
  191. width = strtol(tokstart, &tokend, 10);
  192. tokstart=tokend;
  193. break;
  194. case 'H': // Height. Required.
  195. height = strtol(tokstart, &tokend, 10);
  196. tokstart=tokend;
  197. break;
  198. case 'C': // Color space
  199. if (strncmp("420jpeg",tokstart,7)==0) {
  200. pix_fmt = PIX_FMT_YUV420P;
  201. chroma_sample_location = AVCHROMA_LOC_CENTER;
  202. } else if (strncmp("420mpeg2",tokstart,8)==0) {
  203. pix_fmt = PIX_FMT_YUV420P;
  204. chroma_sample_location = AVCHROMA_LOC_LEFT;
  205. } else if (strncmp("420paldv", tokstart, 8)==0) {
  206. pix_fmt = PIX_FMT_YUV420P;
  207. chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  208. } else if (strncmp("411", tokstart, 3)==0)
  209. pix_fmt = PIX_FMT_YUV411P;
  210. else if (strncmp("422", tokstart, 3)==0)
  211. pix_fmt = PIX_FMT_YUV422P;
  212. else if (strncmp("444alpha", tokstart, 8)==0) {
  213. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
  214. return -1;
  215. } else if (strncmp("444", tokstart, 3)==0)
  216. pix_fmt = PIX_FMT_YUV444P;
  217. else if (strncmp("mono",tokstart, 4)==0) {
  218. pix_fmt = PIX_FMT_GRAY8;
  219. } else {
  220. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
  221. return -1;
  222. }
  223. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  224. break;
  225. case 'I': // Interlace type
  226. switch (*tokstart++){
  227. case '?':
  228. break;
  229. case 'p':
  230. s1->interlaced_frame=0;
  231. break;
  232. case 't':
  233. s1->interlaced_frame=1;
  234. s1->top_field_first=1;
  235. break;
  236. case 'b':
  237. s1->interlaced_frame=1;
  238. s1->top_field_first=0;
  239. break;
  240. case 'm':
  241. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
  242. return -1;
  243. default:
  244. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  245. return -1;
  246. }
  247. break;
  248. case 'F': // Frame rate
  249. sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
  250. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  251. break;
  252. case 'A': // Pixel aspect
  253. sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
  254. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  255. break;
  256. case 'X': // Vendor extensions
  257. if (strncmp("YSCSS=",tokstart,6)==0) {
  258. // Older nonstandard pixel format representation
  259. tokstart+=6;
  260. if (strncmp("420JPEG",tokstart,7)==0)
  261. alt_pix_fmt=PIX_FMT_YUV420P;
  262. else if (strncmp("420MPEG2",tokstart,8)==0)
  263. alt_pix_fmt=PIX_FMT_YUV420P;
  264. else if (strncmp("420PALDV",tokstart,8)==0)
  265. alt_pix_fmt=PIX_FMT_YUV420P;
  266. else if (strncmp("411",tokstart,3)==0)
  267. alt_pix_fmt=PIX_FMT_YUV411P;
  268. else if (strncmp("422",tokstart,3)==0)
  269. alt_pix_fmt=PIX_FMT_YUV422P;
  270. else if (strncmp("444",tokstart,3)==0)
  271. alt_pix_fmt=PIX_FMT_YUV444P;
  272. }
  273. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  274. break;
  275. }
  276. }
  277. if ((width == -1) || (height == -1)) {
  278. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  279. return -1;
  280. }
  281. if (pix_fmt == PIX_FMT_NONE) {
  282. if (alt_pix_fmt == PIX_FMT_NONE)
  283. pix_fmt = PIX_FMT_YUV420P;
  284. else
  285. pix_fmt = alt_pix_fmt;
  286. }
  287. if (raten <= 0 || rated <= 0) {
  288. // Frame rate unknown
  289. raten = 25;
  290. rated = 1;
  291. }
  292. if (aspectn == 0 && aspectd == 0) {
  293. // Pixel aspect unknown
  294. aspectd = 1;
  295. }
  296. st = av_new_stream(s, 0);
  297. if(!st)
  298. return AVERROR(ENOMEM);
  299. st->codec->width = width;
  300. st->codec->height = height;
  301. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  302. av_set_pts_info(st, 64, rated, raten);
  303. st->codec->pix_fmt = pix_fmt;
  304. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  305. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  306. st->sample_aspect_ratio= (AVRational){aspectn, aspectd};
  307. st->codec->chroma_sample_location = chroma_sample_location;
  308. return 0;
  309. }
  310. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  311. {
  312. int i;
  313. char header[MAX_FRAME_HEADER+1];
  314. int packet_size, width, height;
  315. AVStream *st = s->streams[0];
  316. struct frame_attributes *s1 = s->priv_data;
  317. for (i=0; i<MAX_FRAME_HEADER; i++) {
  318. header[i] = get_byte(s->pb);
  319. if (header[i] == '\n') {
  320. header[i+1] = 0;
  321. break;
  322. }
  323. }
  324. if (i == MAX_FRAME_HEADER) return -1;
  325. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  326. width = st->codec->width;
  327. height = st->codec->height;
  328. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  329. if (packet_size < 0)
  330. return -1;
  331. if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
  332. return AVERROR(EIO);
  333. if (s->streams[0]->codec->coded_frame) {
  334. s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  335. s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
  336. }
  337. pkt->stream_index = 0;
  338. return 0;
  339. }
  340. static int yuv4_probe(AVProbeData *pd)
  341. {
  342. /* check file header */
  343. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
  344. return AVPROBE_SCORE_MAX;
  345. else
  346. return 0;
  347. }
  348. #if CONFIG_YUV4MPEGPIPE_DEMUXER
  349. AVInputFormat ff_yuv4mpegpipe_demuxer = {
  350. "yuv4mpegpipe",
  351. NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  352. sizeof(struct frame_attributes),
  353. yuv4_probe,
  354. yuv4_read_header,
  355. yuv4_read_packet,
  356. .extensions = "y4m"
  357. };
  358. #endif