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.

409 lines
13KB

  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. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  30. {
  31. AVStream *st;
  32. int width, height;
  33. int raten, rated, aspectn, aspectd, n;
  34. char inter;
  35. const char *colorspace = "";
  36. st = s->streams[0];
  37. width = st->codec->width;
  38. height = st->codec->height;
  39. av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
  40. aspectn = st->codec->sample_aspect_ratio.num;
  41. aspectd = st->codec->sample_aspect_ratio.den;
  42. if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
  43. inter = 'p'; /* progressive is the default */
  44. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
  45. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  46. }
  47. switch(st->codec->pix_fmt) {
  48. case PIX_FMT_GRAY8:
  49. colorspace = " Cmono";
  50. break;
  51. case PIX_FMT_YUV411P:
  52. colorspace = " C411 XYSCSS=411";
  53. break;
  54. case PIX_FMT_YUV420P:
  55. colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
  56. break;
  57. case PIX_FMT_YUV422P:
  58. colorspace = " C422 XYSCSS=422";
  59. break;
  60. case PIX_FMT_YUV444P:
  61. colorspace = " C444 XYSCSS=444";
  62. break;
  63. }
  64. /* construct stream header, if this is the first frame */
  65. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  66. Y4M_MAGIC,
  67. width,
  68. height,
  69. raten, rated,
  70. inter,
  71. aspectn, aspectd,
  72. colorspace);
  73. return n;
  74. }
  75. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  76. {
  77. AVStream *st = s->streams[pkt->stream_index];
  78. ByteIOContext *pb = &s->pb;
  79. AVPicture *picture;
  80. int* first_pkt = s->priv_data;
  81. int width, height, h_chroma_shift, v_chroma_shift;
  82. int i, m;
  83. char buf2[Y4M_LINE_MAX+1];
  84. char buf1[20];
  85. uint8_t *ptr, *ptr1, *ptr2;
  86. picture = (AVPicture *)pkt->data;
  87. /* for the first packet we have to output the header as well */
  88. if (*first_pkt) {
  89. *first_pkt = 0;
  90. if (yuv4_generate_header(s, buf2) < 0) {
  91. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  92. return AVERROR_IO;
  93. } else {
  94. put_buffer(pb, buf2, strlen(buf2));
  95. }
  96. }
  97. /* construct frame header */
  98. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  99. put_buffer(pb, buf1, strlen(buf1));
  100. width = st->codec->width;
  101. height = st->codec->height;
  102. ptr = picture->data[0];
  103. for(i=0;i<height;i++) {
  104. put_buffer(pb, ptr, width);
  105. ptr += picture->linesize[0];
  106. }
  107. if (st->codec->pix_fmt != PIX_FMT_GRAY8){
  108. // Adjust for smaller Cb and Cr planes
  109. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  110. width >>= h_chroma_shift;
  111. height >>= v_chroma_shift;
  112. ptr1 = picture->data[1];
  113. ptr2 = picture->data[2];
  114. for(i=0;i<height;i++) { /* Cb */
  115. put_buffer(pb, ptr1, width);
  116. ptr1 += picture->linesize[1];
  117. }
  118. for(i=0;i<height;i++) { /* Cr */
  119. put_buffer(pb, ptr2, width);
  120. ptr2 += picture->linesize[2];
  121. }
  122. }
  123. put_flush_packet(pb);
  124. return 0;
  125. }
  126. static int yuv4_write_header(AVFormatContext *s)
  127. {
  128. int* first_pkt = s->priv_data;
  129. if (s->nb_streams != 1)
  130. return AVERROR_IO;
  131. if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
  132. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
  133. }
  134. else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
  135. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
  136. (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
  137. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
  138. 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");
  139. return AVERROR_IO;
  140. }
  141. *first_pkt = 1;
  142. return 0;
  143. }
  144. static int yuv4_write_trailer(AVFormatContext *s)
  145. {
  146. return 0;
  147. }
  148. #ifdef CONFIG_YUV4MPEGPIPE_MUXER
  149. AVOutputFormat yuv4mpegpipe_muxer = {
  150. "yuv4mpegpipe",
  151. "YUV4MPEG pipe format",
  152. "",
  153. "y4m",
  154. sizeof(int),
  155. CODEC_ID_NONE,
  156. CODEC_ID_RAWVIDEO,
  157. yuv4_write_header,
  158. yuv4_write_packet,
  159. yuv4_write_trailer,
  160. .flags = AVFMT_RAWPICTURE,
  161. };
  162. #endif
  163. /* Header size increased to allow room for optional flags */
  164. #define MAX_YUV4_HEADER 80
  165. #define MAX_FRAME_HEADER 80
  166. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  167. {
  168. char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
  169. char *tokstart,*tokend,*header_end;
  170. int i;
  171. ByteIOContext *pb = &s->pb;
  172. int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
  173. enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
  174. AVStream *st;
  175. struct frame_attributes *s1 = s->priv_data;
  176. for (i=0; i<MAX_YUV4_HEADER; i++) {
  177. header[i] = get_byte(pb);
  178. if (header[i] == '\n') {
  179. header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
  180. header[i+2] = 0;
  181. break;
  182. }
  183. }
  184. if (i == MAX_YUV4_HEADER) return -1;
  185. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  186. s1->interlaced_frame = 0;
  187. s1->top_field_first = 0;
  188. header_end = &header[i+1]; // Include space
  189. for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
  190. if (*tokstart==0x20) continue;
  191. switch (*tokstart++) {
  192. case 'W': // Width. Required.
  193. width = strtol(tokstart, &tokend, 10);
  194. tokstart=tokend;
  195. break;
  196. case 'H': // Height. Required.
  197. height = strtol(tokstart, &tokend, 10);
  198. tokstart=tokend;
  199. break;
  200. case 'C': // Color space
  201. if (strncmp("420jpeg",tokstart,7)==0)
  202. pix_fmt = PIX_FMT_YUV420P;
  203. else if (strncmp("420mpeg2",tokstart,8)==0)
  204. pix_fmt = PIX_FMT_YUV420P;
  205. else if (strncmp("420paldv", tokstart, 8)==0)
  206. pix_fmt = PIX_FMT_YUV420P;
  207. else if (strncmp("411", tokstart, 3)==0)
  208. pix_fmt = PIX_FMT_YUV411P;
  209. else if (strncmp("422", tokstart, 3)==0)
  210. pix_fmt = PIX_FMT_YUV422P;
  211. else if (strncmp("444alpha", tokstart, 8)==0) {
  212. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
  213. return -1;
  214. } else if (strncmp("444", tokstart, 3)==0)
  215. pix_fmt = PIX_FMT_YUV444P;
  216. else if (strncmp("mono",tokstart, 4)==0) {
  217. pix_fmt = PIX_FMT_GRAY8;
  218. } else {
  219. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
  220. return -1;
  221. }
  222. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  223. break;
  224. case 'I': // Interlace type
  225. switch (*tokstart++){
  226. case '?':
  227. break;
  228. case 'p':
  229. s1->interlaced_frame=0;
  230. break;
  231. case 't':
  232. s1->interlaced_frame=1;
  233. s1->top_field_first=1;
  234. break;
  235. case 'b':
  236. s1->interlaced_frame=1;
  237. s1->top_field_first=0;
  238. break;
  239. case 'm':
  240. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
  241. return -1;
  242. default:
  243. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  244. return -1;
  245. }
  246. break;
  247. case 'F': // Frame rate
  248. sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
  249. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  250. break;
  251. case 'A': // Pixel aspect
  252. sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
  253. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  254. break;
  255. case 'X': // Vendor extensions
  256. if (strncmp("YSCSS=",tokstart,6)==0) {
  257. // Older nonstandard pixel format representation
  258. tokstart+=6;
  259. if (strncmp("420JPEG",tokstart,7)==0)
  260. alt_pix_fmt=PIX_FMT_YUV420P;
  261. else if (strncmp("420MPEG2",tokstart,8)==0)
  262. alt_pix_fmt=PIX_FMT_YUV420P;
  263. else if (strncmp("420PALDV",tokstart,8)==0)
  264. alt_pix_fmt=PIX_FMT_YUV420P;
  265. else if (strncmp("411",tokstart,3)==0)
  266. alt_pix_fmt=PIX_FMT_YUV411P;
  267. else if (strncmp("422",tokstart,3)==0)
  268. alt_pix_fmt=PIX_FMT_YUV422P;
  269. else if (strncmp("444",tokstart,3)==0)
  270. alt_pix_fmt=PIX_FMT_YUV444P;
  271. }
  272. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  273. break;
  274. }
  275. }
  276. if ((width == -1) || (height == -1)) {
  277. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  278. return -1;
  279. }
  280. if (pix_fmt == PIX_FMT_NONE) {
  281. if (alt_pix_fmt == PIX_FMT_NONE)
  282. pix_fmt = PIX_FMT_YUV420P;
  283. else
  284. pix_fmt = alt_pix_fmt;
  285. }
  286. if (raten == 0 && rated == 0) {
  287. // Frame rate unknown
  288. raten = 25;
  289. rated = 1;
  290. }
  291. if (aspectn == 0 && aspectd == 0) {
  292. // Pixel aspect unknown
  293. aspectd = 1;
  294. }
  295. st = av_new_stream(s, 0);
  296. st = s->streams[0];
  297. st->codec->width = width;
  298. st->codec->height = height;
  299. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  300. av_set_pts_info(st, 64, rated, raten);
  301. st->codec->pix_fmt = pix_fmt;
  302. st->codec->codec_type = CODEC_TYPE_VIDEO;
  303. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  304. st->codec->sample_aspect_ratio= (AVRational){aspectn, aspectd};
  305. return 0;
  306. }
  307. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  308. {
  309. int i;
  310. char header[MAX_FRAME_HEADER+1];
  311. int packet_size, width, height;
  312. AVStream *st = s->streams[0];
  313. struct frame_attributes *s1 = s->priv_data;
  314. for (i=0; i<MAX_FRAME_HEADER; i++) {
  315. header[i] = get_byte(&s->pb);
  316. if (header[i] == '\n') {
  317. header[i+1] = 0;
  318. break;
  319. }
  320. }
  321. if (i == MAX_FRAME_HEADER) return -1;
  322. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  323. width = st->codec->width;
  324. height = st->codec->height;
  325. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  326. if (packet_size < 0)
  327. return -1;
  328. if (av_get_packet(&s->pb, pkt, packet_size) != packet_size)
  329. return AVERROR_IO;
  330. if (s->streams[0]->codec->coded_frame) {
  331. s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  332. s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
  333. }
  334. pkt->stream_index = 0;
  335. return 0;
  336. }
  337. static int yuv4_read_close(AVFormatContext *s)
  338. {
  339. return 0;
  340. }
  341. static int yuv4_probe(AVProbeData *pd)
  342. {
  343. /* check file header */
  344. if (pd->buf_size <= sizeof(Y4M_MAGIC))
  345. return 0;
  346. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
  347. return AVPROBE_SCORE_MAX;
  348. else
  349. return 0;
  350. }
  351. #ifdef CONFIG_YUV4MPEGPIPE_DEMUXER
  352. AVInputFormat yuv4mpegpipe_demuxer = {
  353. "yuv4mpegpipe",
  354. "YUV4MPEG pipe format",
  355. sizeof(struct frame_attributes),
  356. yuv4_probe,
  357. yuv4_read_header,
  358. yuv4_read_packet,
  359. yuv4_read_close,
  360. .extensions = "y4m"
  361. };
  362. #endif